Go4Expert (http://www.go4expert.com/)
-   Web Development (http://www.go4expert.com/articles/web-development/)
-   -   Tweaks & Tricks with .htaccess (http://www.go4expert.com/articles/tweaks-tricks-htaccess-t1720/)

S k 25Oct2006 20:13

Tweaks & Tricks with .htaccess


A really cool feature that web designers overlook is the use of the .htaccess file. Simply put, whenever you see a custom error page (404, 403, etc.), use server side includes, restrict IPs, load a default page, or do any host of events invisible to the user, you are probably going to be using .htaccess.

I will provide further examples of items you can add to an .htaccess file beyond just custom error pages.

.htaccess is a simple text file that you add and edit in a text editor such as Notepad. You save the file as .htaccess without any extra characters before or after the name of the file. I know the file's name is a little strange. Then again, most cool things are.

Anyway, the following paragraphs will try to highlight some of the more useful .htaccess features and the code required to get those features to work. To test the following features, copy and paste the line(s) of code that you want into Notepad, save the file as .htaccess, and upload the file to the root web directory of your web server.

Here goes:

Custom Error Pages

Don't you hate it when people access your site via a bad link and are faced with an error message such as "Permission Denied" or "Page Cannot be Found"? Well, you can liven up the error messages by creating custom error pages for each type of error you anticipate your visitors will encounter:

ErrorDocument 404 http://www.go4expert.com/404.htm
ErrorDocument 401 /admin/authorization.htm
ErrorDocument 403 /password/forbidden.htm

To create custom error pages, replace the error code next to ErrorDocument to the appropriate error code, and change the URL of the page that will receiving the error-prone user! For example, here is this site's 404 error page: http://www.go4expert.com/404.htm


Here is a nightmare scenario. You decide to shift an entire folder's worth of pages and images into a new folder on your server. If you aren't using any 3rd party programs to update any links to the new location, your users are going to be seeing a lot of the Custom error pages you may have used previously.

A solution would be to the .htaccess file's redirect feature. For example, if you are interested in redirecting users from a folder called temp to another folder, you would use the following code:

Redirect /temp http://www.go4expert.com/forums

Where the first argument is the folder or filename that you are trying to redirect people from, and where the second argument is the file or directory you are trying to get people to go to instead:

Redirect old file/folder new file/folder

The above line of code is a template for how the Redirect feature of .htaccess works.

Changing Home Page/Directory Page

When somebody types the URL to your site, more than likely, by default the index.htm or similar page will load first. You can actually use .htaccess and change which file gets loaded first when your URL gets loaded.

DirectoryIndex file.htm

In the above code, replace file.htm with the name of your new default page. When your visitors visit your site by simply typing in the URL of your site, they will be taken to whichever the default page you specify in the place of file.htm in the above code.

IP Blocking

If you would like to prevent an individual or a group of individuals on a certain IP range from accessing your site, you can selectively deny access to them.
You would use the following command format:

    <Limit GET HEAD POST>
    order allow,deny
    deny from
    deny from 18.132.152
    deny from 24.2
    allow from all

Of course, you would probably not be interested in blocking the fictitious IPs I mentioned above. You should change those to something more relevant. If you are really not well liked, you can add as many deny from lines of code as you want.

If somebody from an entire IP range is bugging, you can block all IPs within that range by only entering a smaller portion of their IP such as 24.2. All IPs that being with 24.2 such as and will also be blocked automatically. You may want to be careful for you may not want to block access to visitors that you still want accessing site.

Applying .htaccess to Folders and Sub-Folders

Your .htaccess file doesn't need to be in the root directory, you can put it in any directory - it then applies to the directory you put it in, and all subdirectories.

You can also allow only from certain IPs - for example:

order allow,deny
allow from 123.456.789.012
deny from all

Perhaps not so useful if you're applying it to your whole site, but good when used with other directories.

The above .htaccess tricks are ones that I feel you will enjoy using. Remember, some server hosts may consider using .htaccess a violation of your privileges. Some HTML editing programs may have their own file management and access systems that do not play well when in the presence of .htaccess. Also, IIS servers do not support .htaccess.

rapwaydown 8Dec2007 06:00

Re: Tweaks & Tricks with .htaccess
good articles, do you mind if i use this on my website, of course is creadit to you

kien_vn 31Aug2010 19:13

Re: Tweaks & Tricks with .htaccess
This article is simple but effective for beginners.

linux7802 10Sep2010 14:52

Re: Tweaks & Tricks with .htaccess
Yes its useful for beginners as well as for professionals...

PradeepKr 11Sep2010 01:34

Re: Tweaks & Tricks with .htaccess
This would be helpful in blocking bad bots/crawlers which eat up lot of bandwidth.
Identify the IP of the bad bot and deny them :)

parrytint 11Oct2010 11:47

Re: Tweaks & Tricks with .htaccess

How far the google shorten url is benefits like twitter..??? meet again.

All times are GMT +5.5. The time now is 21:18.