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
I'd like to use mod_rewite/.htaccess do the following:
for the same site
Grab the Apache Mod_Rewrite Documentation. Also http://whathaveyoutried.com.
You can check the status of HTTPS
by using RewriteCond {%HTTPS} on
(or !on
)
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
.
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