DevNotes \ With WWW and Without WWW: Redirect Rules For Apache and IIS

Apache

To redirect all users to access the site with the 'www.' prefix:

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

To redirect all users to access the site without the 'www.' prefix:

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

IIS6 With ISAPI Rewrite

Very similar to Apache. To redirect all users to access the site with the 'www.' prefix:

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

To redirect all users to access the site without the 'www.' prefix:

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

© Alexey Busygin, 2008–2010. alexey@busygin.com