The code below is a redirecting script that redirects visitors of my website to http://www.domain.com if they are not on it (like for example if they are on domain.com or somesub.domain.com). I modified it, to redirect them to either HTTPS or HTTP, depending on the link that they typed or got (hot)linked to.
It is working, but i’m guessing there could be an easier way to do this or that there is some room for improvement. Can someone double check / comment on this? Many thanks in advance.
# redirect any HTTP traffic that is not http://www.domain.com/*
RewriteEngine On
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
# redirect any HTTPS traffic that is not https://www.domain.com/*
RewriteEngine On
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
1 Answer