How to make your site redirect with and without the 'www'

Written by Tony Lea on Jan 18th, 2011 Views Report Post

Using your .htaccess you can do a lot of things, such as making SE Friendly URL's. One more great trick is the ability to redirect your site so that it always points to the www version or without. Let's say that your base URL for your site is linking all your CSS or JS files from the www version of your site, in that case you would want all your pages to redirect to the www version of your site. for example (www.example.com as opposed to just example.com)

Well it's very simple, to add the www to your site, just enter the following code in your .htaccess file

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

Alternatively, if you wanted to remove the www from your site so that way they always end up at yoursite.com as opposed to www.yoursite.com you would add the following code to the .htaccess:

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

And, that's it! Simple enough. ;)

Comments (0)