Use .htaccess to force a particular URL
For most websites, the ‘www’ preffix is optional, and many have multiple domain names. This site is an example, as I have the domain names leavetobrew.com and leavetobrew.co.uk pointing at the same space, with ‘www’ being optional for both.
I want to enforce one particular URL for my domain, and I’m going to use an .htaccess file to do it.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.leavetobrew.com [NC]
RewriteRule ^(.*)$ http://www.leavetobrew.com/$1 [R=301,L]
</IfModule>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.leavetobrew.com [NC]
RewriteRule ^(.*)$ http://www.leavetobrew.com/$1 [R=301,L]
</IfModule>
You need to have mod_rewrite enabled for your domain for this to work. 301 permanent redirects are nice and search-engine friendly, as a search engine visiting the “wrong” URL will be encouraged to permanently update its records for that page.


