Todays .htaccess tip – How to get rid of the www. subdomain

#www. is overrated
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*)  http://%1/$1 [QSA,L,R=301]

Now let’s explain how this works:

The first line is a comment, that explains what the rewrite is supposed to do.

This line just turns on the rewrite engine

RewriteEngine On

This line says if the domain is “www.anything” and note that it saves the domain without the www. part as %1

RewriteCond %{HTTP_HOST} ^www\.(.*)

now this line does the actual rewriting. from anything to the domain with the query string attached (QSA) so we won’t break links. also note that it is a permanent redirect (the R=301 part) and it’s the last rule (L) that will be executed for this request.

RewriteRule ^(.*)  http://%1/$1 [QSA,L,R=301]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.