Problems encountered:
I want to redirect www.example.com to example.com. The following htaccess code for this purpose:
{}% ^ HTTP_HOST the RewriteCond www.example.com [the NC]
the RewriteRule ^ (. *) $ Http://example.com/$1 [L, R & lt = 301]
However, there may be a method in a general manner do not have to hard-code domain name?
solution:
A Solution
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
And Michael are the same, except this works: P
Solution two
However, if we need to do this to separate http and https:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Third solution
Redirect the non-www to www (both: http + https)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Original link: http://findmybug.cn/article/generic-htaccess-redirect-www-to-non-www