use mod_rewrite .htaccess to add www to http:// while removing www from https://

763 views Asked by At

I'd like to use mod_rewite/.htaccess do the following:

  • add www to http urls
  • remove www from https urls

for the same site

1

There are 1 answers

3
Scott Stevens On
  1. Grab the Apache Mod_Rewrite Documentation. Also http://whathaveyoutried.com.

  2. You can check the status of HTTPS by using RewriteCond {%HTTPS} on (or !on)

  3. You can check for www. on the host using RewriteCond {%HTTP_HOST} ^www\.(.*)$ (or !^www\.(.*)$). The pattern matched is in %n, and RewriteRule backreferences are $n.

  4. You can rewrite URLs using RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]. By switching http for https and removing the www., you can change this to fit the HTTPS rule.

The final code:

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

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

And I'll say it again, http://whathaveyoutried.com