The .htaccess is a powerful tool when it comes to managing server-side actions. It is a common comment that sites that have both www.domain.com and domain.com can get penalised from search engines because they are seen as 2 different websites. Personally I think this theory is a bit silly and you’d think search engines such as google would be smarter than that. Never the less, site addresses should probably be consistent anyway. Using .htaccess you can redirect traffic that goes to domain.com to www.domain.com – thus fixing the search engine problem and making your domain consistant to all visitors.
Add the code to the .htaccess file
To redirect visitors from the www.-less domain we add the following code to our .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^yourdomain.com [nc]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [r=301,nc]
Make sure you replace yourdomain.com with your domain. This is all you need to do! Easy!
About .htaccess files
.htaccess files have a number of uses, from changing apache set-ups to editing php settings. It’s quite useful however it can be a fiddly process. The reason the files start with a “.” instead of having the normal format is because they typically are hidden files. Linux and Mac OS X typically treat these files as hidden. If you edit locally this can cause problems if you haven’t set your computer to show hidden files. Programs such as Coda allow you to show/edit these hidden files inside it’s file browser.
I recently had a series of my sites hacked with .htaccess files that added a series of porn links to my site in the source code (the sites remained as were). It’s a really tricky thing to discover as they were old sites that I don’t commonly edit so it wasn’t until it was pointed out to me that I found it out. So if you have a similar hack done to one of your sites delete the .htaccess file!
Setting up a .htaccess file
Easy, just create a file called .htaccess in your favourite text editor. Save it to the root directory of your domain (or the directory you wish the actions to occur). If it doesn’t work, make sure your server has .htaccess scripting allowed.
I’ve got a lot more .htaccess tips to share so keep a look out.




