Add www to all URL's on Apach website (.htaccess mod_rewrite)

Following rewrite rule will add www to all URL's on your website in case a visitor didn't type www or followed a link without www. Add this to your .htaccess file:

 
Options +FollowSymLinks
RewriteEngine On
rewritecond %{HTTP_HOST} ^yourdomain.com$ [nc]
rewriterule ^(.*)$ http://www.yourdomain.com/$1 [l,r=301,nc]
 

Syntax:

rewritecond - defines rewrite condition
%{HTTP_HOST} - Server variable
^ - start matching
$ - end matching
[nc] - non case sensitive
rewriterule - defines rewrite rule
(.*) - match everything
$1 - append original URL part captured in (.*)
l - last rule (only omit in linked rules)
r=301 - redirect using 301 response (permanent redirect)

Newsletter

Subscribe to receive occasional updates on new posts.
Your email will not be used for any other purpose and you can unsubscribe at any time.
Please wait