Website Design Principles 2025

Read More

Redirect www.domain.com to domain.com using .htaccess

26th June, 2011

Just a quick blog post to share a cool little trick; how to redirect www.domain.com to domain.com using .htaccess

Ok, an Apache Web server supports URL rewriting with the mod_rewrite engine, so if you place various rules within a .htaccess file on your server, you can assist both search engines (who can usually figure out which format is preferred) and people who visit your site.

How to redirect www.domain.com to domain.com

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

And if you wanted to re-direct domain.com to www.domain.com then you would use

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

Please note that both of these rules are search engine friendly, plus if you have pages within your site such as about.html or contact.php then the rule will also apply to them! There is also no right or wrong when it comes to domain.com or www.domain.com, it is personal preference!