I'm trying to work out a .htaccess rule that I can use on my main domain & subdomains without requiring changes to the code for each one.
I've tried the following for a subdomain to try and force https and non-www but it doesn't work correctly.
<IfModule mod_rewrite.c>
# Force HTTPS & NON-WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.test\.website\.com$ [NC] # Detect if it has www?
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^test\.website\.com$ [NC]
RewriteRule ^ https://test\.website\.com%{REQUEST_URI} [R=301,L]
</IfModule>
Currently if I was to go to http://www.test.website.com/test/
it redirects to https://test%2Cwebsite.com/
so it sort of works but not quite.
I've been trying to do it more dynamically too using:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on [NC]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>
But this isn't working at all. What's the best solution to achieve this?
It should essentially be forcing all of the following:
http://website.com/ => https://website.com/
http://www.website.com/ => https://website.com/
https://www.website.com/ => https://website.com/
http://website.com/testing/ => https://website.com/testing/
http://www.website.com/testing/ => https://website.com/testing/
https://www.website.com/testing/ => https://website.com/testing/
http://test.website.com/ => https://test.website.com/
http://www.test.website.com/ => https://test.website.com/
https://www.test.website.com/ => https://test.website.com/
http://test.website.com/testing/ => https://test.website.com/testing/
http://www.test.website.com/testing/ => https://test.website.com/testing/
https://www.test.website.com/testing/ => https://test.website.com/testing/
You can use this single dynamic rule to achieve all http -> https
and www -> non-www
redirections:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Make sure this rule is right at top and you clear your browser cache before testing this change.
Try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# First, force the HTTPS, whatever it is:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Then drop the www, if any:
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule .* https://%1%{REQUEST_URI} [L,R=301]
</IfModule>
## Results:
# http://website.com/ => https://website.com/
# http://www.website.com/ => https://website.com/
# https://www.website.com/ => https://website.com/
# http://website.com/testing/ => https://website.com/testing/
# http://www.website.com/testing/ => https://website.com/testing/
# https://www.website.com/testing/ => https://website.com/testing/
# http://test.website.com/ => https://test.website.com/
# http://www.test.website.com/ => https://test.website.com/
# https://www.test.website.com/ => https://test.website.com/
# http://test.website.com/testing/ => https://test.website.com/testing/
# http://www.test.website.com/testing/ => https://test.website.com/testing/
# https://www.test.website.com/testing/ => https://test.website.com/testing/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With