1

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 .* https://www.example1.com%{REQUEST_URI} [R,L]

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

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

it didn't work. Please help with solution.

1

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 .* https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

The HTTP_HOST server variable contains the hostname from the request. eg. example1.com, example2.com, www.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 .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

# Redirect HTTP to HTTPS on the same host
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{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).

  • RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L] – Rivash Harripersad Feb 11 '18 at 14:31
  • I tried that piece of code. But the issue I'm having is that it's only working for the primary domain in the public_html . the other two addon domains are not redirecting. – Rivash Harripersad Feb 11 '18 at 14:33
  • The add-ons are not in the public_html folder and I cant have them there because I'm assuming the link to access it will become something like www.example1.com/www.example2.com – Rivash Harripersad Feb 11 '18 at 14:37
  • Also, i tried your code. It's only working for the primary ( in public_html ) it's not redirecting for the other domains. – Rivash Harripersad Feb 11 '18 at 14:45
  • 1
    it works....... – Rivash Harripersad Feb 11 '18 at 16:43
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.