Add www. in URL w/ mod_rewrite

Just a quick piece of code that I put into every .htaccess on every site I run. This uses Apache’s mod_rewrite module and is as follows:

RewriteEngine On
Rewritecond %{http_host} ^$ [NC]
Rewriterule ^(.*)$$1 [r=301,NC]

That adds in the www. to your URL if someone just tries to access your website without the www. prepended. Alternatively, you can force remove the www.

Posted by Will on May 20, 2012

Leave a Reply

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

TechJunkie Also Recommends