Skip to Navigation

Home -> Articles & Tutorials -> .htaccess -> The Start

Tutorial -:- A Short Guide to using the .htaccess file -

This tutorial will guide you through some of the uses of the '.htaccess' file. This tutorial spans 11 pages. You will find a navigation menu at the bottom of every page. Now we begin.

You might have heard of a little file called .htaccess. If you have you might even know some of the things it can do. This little file can actually be used for quite a few things.

We will go into these in more detail further on in this tutorial.

For instance you probably know that this file is use to implement custom error pages or password protected directories. But there is much more available to you through the marvelously simple .htaccess file.

 

 

A Few General Ideas

An htaccess file is an ASCII file, such as you would create through a text editor like Notepad or wordpad. Many people get confused over the naming of the file, so let me get that out of the way.

.htaccess is the file extension. It is not file.htaccess or somepage.htaccess, it is simply named .htaccess

To create the file, open up a text editor and save an empty page as .htaccess. Chances are that your editor will append its default file extension to the name (ex: for Notepad it would call the file .htaccess.txt). You need to remove the .txt (or other) file extension in order to get it to work. The easiest way to do this is to right click the file in Windows Explorer and delete everything in the name except .htaccess

Viola you now have a htaccess file.

htaccess files must be uploaded as ASCII mode, not BINARY. This should be done by your FTP client but if you get any errors later it would be wise to check this. You should CHMOD the htaccess file to 644 or (RW-R--R--).

All this means is that the server can see the file but it can not be seen in a browser. If you don't it can seriously compromise your security. (For example, if you have password protected directories, if a browser can read the htaccess file, then they can get the location of the authentication file and get a list of usernames and passwords. We will cover security a bit later on any way so don't worry to much about it for now.

All commands in htaccess should be placed on one line only, so if you use a text editor that uses word-wrap, make sure it is turned off or it might throw in a few characters that really confuse Apache to no end.

htaccess is only used on Apache servers, not on NT. So you need to make sure you website is running on Apache before you try to implement any thing in this tutorial. You hosting service can tell you if you are unsure.

htaccess files normally affect the directory they are placed in and all sub-directories,

IE:
www.yoursite.com/.htaccess would affect
www.yoursite.com/folder1,
yoursite.com/folder1/folder2/, etc.

But this can be prevented. If for example you did not want certain htaccess commands to affect a specific directory by placing a new htaccess file within the directory you don't want affected will over ride this and removing the specific command(s) from the folder. In short, the htaccess file in the current directory is treated as the main htaccess file.

It is possible to cause an infinite loop of redirects or errors if you place something weird in the htaccess. This would trap a browser and really annoy your visitors. They probably would not return to your site again. So take care and always check your site after you make any changes to the .htaccess files. Remember to clear you browser cache first to make sure you are seeing the new site not a locally stored copy.

Some host will not allow users to use the .htaccess files because they can (if configured incorrectly) slow down or otherwise affect the performance of a server. It is important that you make sure you are allowed to use the .htaccess file so you won't get in to trouble.

ON we go then to the important stuff..

This tutorial was has been reprinted by kind permission of Simon at http://liveinthemix.com