Forum Topic: Redirect all http AND https non-www URLS to https://www.example.com via htaccess
I would like to ensure that all traffic to my site to use SSL but I keep coming up against redirection loops.
I have tried:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www\.example\.com/$1
RewriteCond %{ENV:HTTPS} !=on
RewriteRule (.*) https://www\.example\.com/$1
Can anyone point me in the right direction?
5 Replies to “Redirect all http AND https non-www URLS to https:…”
I have fixed this with this:
RewriteEngine On
#Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www\.example\.com/$1
#Redirect http to https
RewriteCond %{ENV:HTTPS} !=on
RewriteRule (.*) https://www\.example\.com/
but this resulted in redirected http://example.com
to http://example.com/https://www.example.com/
So I added one last rule:
Redirect 301 /https://www.example.com/ https://www.example.com/
Crude but it works!
Nice! And I think you can simplify further with something like this:
#Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule .* https://www\.example\.com/$1
#Redirect http to https
RewriteCond %{ENV:HTTPS} !=on
RewriteRule .* https://www\.example\.com/
The main difference here is the added s
in the first rewrite rule. That should make it so that the second rewrite rule doesn’t happen, which should mean that Redirect 301 /https://www.example.com/ ...
shouldn’t be necessary (psuedo-code, haven’t tested).
The issue I have found is http://www.example.com/page1.html
redirects to https://www.example.com/
rather than https://www.example.com/page1.html
I’m stumped. Any ideas?
GOT IT!
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [OR]
RewriteCond %{ENV:HTTPS} !=on
RewriteRule .* https://www\.example\.com%{REQUEST_URI} [R=301,L]
# Walks away, opens a beer, sits with a smile!
Hehe :) Nice one!