Tuesday, 4 October 2016

apache - Generic htaccess redirect www to non-www




I would like to redirect www.example.com to example.com. The following htaccess code makes this happen:



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


But, is there a way to do this in a generic fashion without hardcoding the domain name?


Answer



RewriteEngine On

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


Same as Michael's except this one works :P


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...