Wednesday, 17 June 2009

htaccess sample file

RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$1 [R=301,L]

RewriteEngine on
RewriteRule ^(.*)\$ $1.php

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/\view$ allbox.php?ur=$1
RewriteRule ^([a-zA-Z0-9_-]+)/\groups$ allgroups.php?ur=$1
RewriteRule ^([a-zA-Z0-9_-]+)/\following$ allfollowing.php?ur=$1
RewriteRule ^([a-zA-Z0-9_-]+)/\followers$ allfollowers.php?ur=$1
RewriteRule ^([a-zA-Z0-9_-]+)/\trash$ inbox.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/\tras$ newbox.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/\group$ groupbox.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/\inbox$ mybox.php?id=$1

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)\/bookmarks$ pubbook.php?ur=$1

Custom Error Pages

The first use of the .htaccess file which I will cover is custom error pages. These will allow you to have your own, personal error pages (for example when a file is not found) instead of using your host's error pages or having no page. This will make your site seem much more professional in the unlikely event of an error. It will also allow you to create scripts to notify you if there is an error (for example I use a PHP script on Free Webmaster Help to automatically e-mail me when a page is not found).

You can use custom error pages for any error as long as you know its number (like 404 for page not found) by adding the following to your .htaccess file:

ErrorDocument errornumber /file.html

For example if I had the file notfound.html in the root direct
ory of my site and I wanted to use it for a 404 error I would use:

ErrorDocument 404 /notfound.html

If the file is not in the root directory of your site, you just need to put the path to it:

ErrorDocument 500 /errorpages/500.html

These are some of the most common errors:

401 - Authorization Required
400 - Bad request
403 - Forbidden
500 - Internal Server Error
404 - Wrong page

Then, all you need to do is to create a file to display when the error happens and upload it and the .htaccess file.

Deny/Allow Certian IP Addresses
You can block an IP address by using:

deny from

You can allow an IP address by using:

allow from

If you want to deny everyone from accessing a directory, you can use:

deny from all


Alternative Index Files

You may not always want to use index.htm or index.html as your index file for a directory, for example if you are using PHP files in your site, you may want index.php to be the index file for a directory. You are not limited to 'index' files though. Using .htaccess you can set foofoo.blah to be your index file if you want to!

Alternate index files are entered in a list. The server will work from left to right, checking to see if each file exists, if none of them exisit it will display a directory listing (unless, of course, you have turned this off).

DirectoryIndex index.php index.php3 index.html index.htm


One of the most useful functions of the .htaccess file is to redirect requests to different files, either on the same server, or on a completely different web site. It can be extremely useful if you change the name of one of your files but allow users to still find it. Another use (which I find very useful) is to redirect to a longer URL, for example in my newsletters I can use a very short URL for my affiliate links. The following can be done to redirect a specific file:

Redirect /location/from/root/file.ext

In this above example, a file in the root directory called oldfile.html would be entered as:


and a file in the old subdirectory would be entered as:


You can also redirect whole directoires of your site using the .htaccess file, for example if you had a directory called olddirectory on your site and you had set up the same files on a new site at: you could redirect all the files in that directory without having to specify each one:

Redirect /olddirectory

Then, any request to your site below /olddirectory will bee redirected to the new site, with the
extra information in the URL added on, for example if someone typed in:

They would be redirected to:

This can prove to be extremely powerful if used correctly.

No comments:

Post a Comment