Using Apache Mod Rewrite to redirect from HTTP to HTTPS and from a different TLD
If there is a single domain and you want to remove www and force https so the final domain will be in the format:
https://domain.com
In .htaccess:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://domain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [L,R=301]
If the domain has a secondary top level domain such as .co.uk, you will need to ensure that secondary domain has a valid ssl cert if you want to redirect from that.
Use the following rules to also handle redirects from a second TLD to the main TLD:
RewriteCond %{HTTP_HOST} ^domain.co.uk
RewriteRule ^(.*)$ https://domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://domain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [L,R=301]
You will need to test all the rules with the following urls which iterates through all permutations (The following include some Drupal specific URIs for good measure too, and also assumes you have an about page). All the requests should resolve to https://domain.com/uri
- https://domain.com/
- https://www.domain.com/
- https://domain.com/about
- https://www.domain.com/about
- https://domain.com/user/login
- https://www.domain.com/user/login
- https://domain.co.uk/
- https://www.domain.co.uk/
- https://domain.co.uk/about
- https://www.domain.co.uk/about
- https://domain.co.uk/user/login
- https://www.domain.co.uk/user/login
- http://domain.com/
- http://www.domain.com/
- http://domain.com/about
- http://www.domain.com/about
- http://domain.com/user/login
- http://www.domain.com/user/login
- http://domain.co.uk/
- http://www.domain.co.uk/
- http://domain.co.uk/about
- http://www.domain.co.uk/about
- http://domain.co.uk/user/login
- http://www.domain.co.uk/user/login
Comments
Post a Comment