Adding www to all urls for a domain
On pretty much every of my projects (which I run on apache) I have the need to automatically rewrite all requests without www to the corresponding adress with www. It's simply, and this is the code I use (over, and over again). It rewrites (for example) http://luradittbarn.se to http://www.luradittbarn.se. If you request a page (i.e) http://luradittbarn.se/archives.html to http://www.luradittbarn.se/archives.html
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI}\\/%{HTTP_HOST}/www. ^/+(.+/)?[^.]*[^/]\\(/)([^w][^w][^w][^.].*/(www\.)¦.*)$ [OR,NC]
RewriteCond %{HTTP_HOST}/www. ^(/)?(/)?([^w][^w][^w][^.].*/(www\.))$ [NC]
RewriteRule ^ http://%4%{HTTP_HOST}%{REQUEST_URI}%2 [L,R=301]
The code is originally from Webmasterworlds forum (a very old thread, which can be found here)

Leave a comment