Common .htaccess Tricks

by kirupa  |  17 March 2004 (Updated 8 March 2011)

  Have questions? Discuss this HTML tutorial with others on the forums.

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.

This article is an addendum to the Custom Error Pages tutorial created by redViper. 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. Here is how the file looks when uploaded to a web server:

[ notice the strange file naming convention ]

Anyway, the following sections 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.

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.kirupa.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.

Redirects
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 //www.kirupa.com/developer

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:

order allow,deny
deny from 18.52.3.5
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 24.2.35.3 and 24.2.142.122 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 and it applies to the directory you put it in and all subdirectories. This allows you to specify custom settings on a per directory basis if you really want it.

For example, you could specify that only users from one IP are allowed to access a particular directory. In that case, the htaccess file in that directory would contain something that looks as follows:

order allow,deny
allow from 123.456.789.012
deny from all

This is perhaps not so useful if you're applying it to your whole site, but good when you want to protect a single directory!

Conclusion
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 files.

Thanks to njs12345 for providing some additional .htaccess insights with regards to sub-folders/folders and IIS servers.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.