Typical .htaccess File


Is there such a thing as a typical .htaccess file? There is for me, as I tend to do exactly the same things in each one I create for the myriad of websites I run.

.htaccess Tasks

These are the tasks I need my .htaccess file to perform:

  • redirect the non-www version of my urls to the www version. For example, if you type in http://.sausagetools.com/typical-htaccess-file/ into your address bar, I want the url redirected to http://www.sausagetools.com/typical-htaccess-file/. This has SEO implications, of which much has been written.
  • redirect missing pages. I like to buy expired domains and renovate them. On any particular domain, there will usually be some urls I have no intention of recreating, so I will redirect them either to the home page, or an "equivalent" page.

To accomplish the two tasks above, I use the following code in my .htaccess file:

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

The last line of the above code will redirect the url http://www.sausagetools.com/koalabears.html to http://www.sausagetools.com.