Jens Segers on

Redirect users to www using htaccess

On all my websites I redirect all users to the domain including the www prefix. I prefer it this way just because I think it looks cleaner. I have read that it might have an effect on google's pagerank but I doubt it. So what it does is redirect all users who visit mywebsite.com to www.mywebsite.com.

The code is placed in a file called .htaccess which is in fact a directory-level configuration file that allows for decentralized management of web server configuration. The .htaccess file is placed inside the web tree, and is able to override a subset of the server's global configuration. It can be used for authorisation, rewriting urls, blocking users, SSL, cache control, ...

Create a file called ".htaccess" in your main web folder. Windows might not allow you create this file but you can work this around by giving it a temporary name and then renaming it through other programs such as filezilla (or cmd). Place this code inside the file:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

Adjust the code above to use your domain name instead of mywebsite.com and give it a try. You can read more about the .htaccess file on these websites:

http://httpd.apache.org/docs/2.2/howto/htaccess.html
http://en.wikipedia.org/wiki/Htaccess

Webmentions

Tweet about this blog post and you will appear below!