Mobile app version of vmapp.org
Login or Join
Kevin317

: How to use .htaccess to redirect http to https for 3 domains cPanel I have 3 domains. 2 are Addon domains, 1 primary on my cPanel. I do not know if this is possible but from .htaccess how

@Kevin317

Posted in: #Htaccess #Https #Redirects

I have 3 domains. 2 are Addon domains, 1 primary on my cPanel. I do not know if this is possible but from .htaccess how can I set this up:


example1.com (Primary)
example2.com
example3.com


This is what I want to do:


example1.com > HTTP to HTTPS
example2.com > HTTP to HTTPS
example3.com > HTTP to HTTPS


From the .htaccess on cPanel I tired doing this:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* www.example1.com%{REQUEST_URI} [R,L]

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* www.example2.com%{REQUEST_URI} [R,L]

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* www.example3.com%{REQUEST_URI} [R,L]


it didn't work. Please help with solution.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Kevin317

2 Comments

Sorted by latest first Latest Oldest Best

 

@Welton855

The behavior you describe is normal. An .htaccess files applies to the folder it is in and recursively to subdirectories. Another .htacess can also exist in the subdirectory in which case it has precedence.

In your case you either need to split the file into three, putting the relevant third in an .htaccess within the right directory from where your add-on domains are served.

Or you may be able to put the .htaccess in the parent directory of all three domains, if there is such a directory, and the server is configured to read .htaccess files above public_html. In my server it does work like that but it is possible this depends on a config option.

In any case, splitting the .htaccess is best since it will make things easier in the future should you move servers, change servers for some of the domains, etc.

10% popularity Vote Up Vote Down


 

@Ann8826881

cPanel is notoriously bad for managing redirects. (I didn't think you could create HTTP to HTTPS redirects using cPanel alone?)


I do not know if this is possible...


Probably not from cPanel. But this is relatively straightforward in .htaccess (by directly editing the file itself). To be honest, if something of this nature is not possible in .htaccess then it's probably not something you should be doing in the first place.

If you simply want to redirect from HTTP to HTTPS on the same hostname then all you need is something like:

RewriteEngine On

# Redirect HTTP to HTTPS on the same host
RewriteCond %{HTTPS} !=on
RewriteRule .* %{HTTP_HOST}%{REQUEST_URI} [R=302,L]


The HTTP_HOST server variable contains the hostname from the request. eg. example1.com, example2.com, example1.com. Note that this alone does not canonicalise the www subdomain.

This is also a 302 (temporary) redirect (the same as simply specifying the R flag by itself). Change to a 301 (permanent) only when you are sure it's working OK. (301s are cached hard by the browser so can make testing problematic.)

To canonicalise the www subdomain and redirect non-www to www then add an additional directive:

RewriteEngine On

# Redirect non-www to www (and HTTPS) on the same domain
RewriteCond %{HTTP_HOST} !^www.
RewriteRule .* www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

# Redirect HTTP to HTTPS on the same host
RewriteCond %{HTTPS} !=on
RewriteRule .* %{HTTP_HOST}%{REQUEST_URI} [R=302,L]


This does assume that you don't have any other subdomains (other than www) on these domains.

Note that whilst this might at first look like two redirects (since there are two separate directives), only one redirect will result, since the first non-www to www redirect always redirects to HTTPS (so the second redirect would never occur in this case).

No need to repeat RewriteEngine more than once.

UPDATE:


The add-ons are not in the public_html folder ...


If the Addon domains do not point to the (main domains) public_html folder, or a subdirectory of the public_html folder then you will need to create a separate .htaccess in the document root for each Addon domain, since they are completely separate entities. The above directives would then work for the Addon domains .htaccess file(s).

However, if the Addon domains point to subdirectories off the main domain's document root (a common default in cPanel's setup). eg. public_html/www.example2.com/. Then the above (single) .htaccess file in the public_html folder would still work...

UNLESS you already have additional .htaccess files (that also use mod_rewrite) in the Addon domain's document root directories. This is because mod_rewrite directives are not inherited by default. Now, depending on your config, you could enable mod_rewrite inheritance (but stress that this would depend on your config and what other directives you already have). Otherwise, you will need to duplicate these directives in the .htaccess file for each Addon domain - the same as above (as if your Addon domains were in different parts of the filesystem).

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme