Force a www Prefix

Problem:

You want to force the World Wide Web prefix of your domain name, even though a page was requested without it. For example, if you would like all requests to http://hans-eric.com be redirected to http://www.hans-eric.com

Solution:

Solution from Alister Cameron. It works for Linux/Unix and requires .htaccess file access.
In the web root directory .htaccess file, add the following:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.alistercameron.com$ [NC]
RewriteRule ^(.*)$ http://www.alistercameron.com/$1 [R=301,L]RewriteCond %{REQUEST_URI} ^/[^.]+[^/]$

RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
</IfModule>

If you are using WordPress, those rules should come before the standard WordPress rules.

RSS feed | Trackback URI

Comments »

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> in your comment.