How to use the .htaccess file. Frequently Asked Questions

What is an htaccess file?

The htaccess file is a simple ASCII file which you can create using a regular text editor like NotePad. This file allows us to make configuration changes on a per directory basis. This file works on both Windows and Unix/Linux platforms with Apache Web Server.

Note: htaccess is not the name of a file. It is simple a file extension like .mp3. Only in this case, it is not something.htaccess or file.htaccess. It is simply named .htaccess. Also, if you are modifying an existing .htaccess file,make sure you make a backup of it somewhere before proceeding.

What can I use the htaccess file for?

There are several benefits of using the htaccess file for webmasters and developers. Most of them provide enhanced security to your site. You can prevent directory browsing, password protection for directories, change the default index page of a directory, redirect visitors for one page or directory to another and also prevent hot linking of images on your website.

How to create the htaccess file?

Open up a simple text editor, say Notepad and save it as .htaccess. While saving in say Notepad, remove under the save as option for .txt and save it under all files. Also remember to save under the ASCII mode and not Binary. Once uploaded to your server, make sure the permissions for your .htaccess file are 644 or (RW-R–R–). Having this makes the file writable by the server but not by others including visitors to your site.

Once created, where do I upload my .htaccess file?

Since the .htaccess allows us to drill down and implement changes on a per directory basis , there are multiple options for you to place this file. The only thing you need to remember is that a htaccess file in the root of your site affects your whole site whereas an htaccess file in your images folder is unique to that folder and its subdirectories if any.

/.htaccess(in the root of your site)

/content/.htaccess(in any content site, say membersonly/.htaccess)

/content/images/.htaccess(in an images folder)

If you liked this article, click here to buy me a beer!

Dear visitor, Happy New Year to you and thanks for dropping by. If you enjoyed reading this post, you may want to subscribe to my RSS feed. Thanks for visiting!


Related Posts

Comments

One Response to “How to use the .htaccess file. Frequently Asked Questions”

  1. Dave on January 18th, 2008 7:17 pm

    The reason for the filename starting with a dot is that in Unix, and filename starting with a dot is “invisible”. That is, it won’t be listed when you use the “ls” command unless you specifically ask “ls” to list invisible files.

    Probably the most frequent question I get about .htaccess files is “Why isn’t mine working ?”. The most frequent reason for that is that the .htaccess files are not being used. In your Apache config, add a line that says:

    AllowOverride All

    and, after a restart, .htaccess files will actually be used rather than being ignored. On the other hand, if you can edit your own Apache config files, then anything you can do in a .htaccess file you can do in your Apache config files.

    There are both advantages and disadvantages of both methods. .htaccess files cause an additional overhead to Apache as, for every request, it needs to check for the .htaccess file in the same directory and in the directory above, and the one above that. Also, because there are so many different files, the relationships between them can get quite complex and can lead to some very difficult debugging. Having all of your config options in a single file helps you see everything more clearly.

    When you modify a .htaccess file, the changes are made effective immediately as opposed to an Apache config file which requires that you restart Apache. .htaccess files can also be owned by any user, including the user that your webserver runs as, meaning that you don’t need root access to your webserver to change them. You can even have your PHP code changing your rewrite rules on the fly !

Got something to say?