Redirect www to non-www https.

August 1, 2015 861 views

I am trying to redirect all traffic from http/https www to https only non-www. I have got http to https setup using the following on apache 2.4:

        RewriteCond %{HTTPS} off
        RewriteCond %{HTTP:X-Forwarded-Proto} !https
        RewriteRule ^(.*)$ https://*%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

It also needs to work inside of the virtual hosts conf file and not .htaccess as my cms could overwrite the .htaccess on upgrades. I want to add www redirect as my site may have users that are used to typing www. Thanks for all of your help in advance.

1 Answer

Hey there,

Here is a great tutorial that outlines redirecting www subdomains to non-www URL's.

Here's an example of the rewrite rule that should work and still forward to https:

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Happy coding,

Jon Schwenn
Platform Support Specialist

Have another answer? Share your knowledge.