Redirect * .domain.com to www.domain.com with HTTP or HTTPS prefix

Below is a redirect script that redirects visitors to my site to www.domain.com if they are not on it (for example if they are on a .com or somesub.domain.com domain). I changed it to redirect them to HTTPS or HTTP, depending on the link they typed or got (hot) linked to.

It works, but I'm guessing there might be an easier way to do it, or that there is room for improvement. Can anyone check / comment on this? Thank you very much 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]

      

+3


source to share


1 answer


RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule .* http%1://www.domain.com/$0 [R=301,L]

      



+3


source







All Articles