Design & Development

Get To Know Your .htaccess File

A valuable tool many web developers either don’t understand or aren’t aware of is the .htaccess file. The file is available only to websites hosted on servers running the Apache web server, which is the overwhelming majority of hosting servers. An .htaccess file is a small, plain-text file residing in any directory of a website, and can contain directives to adjust the way the Apache web server reacts. The configuration changes apply to the directory that the .htaccess file is saved in, and to any directories that it might contain. To better understand them, let’s take a look at some of the things web developers can do using .htaccess files.

Uses for the .htaccess file

Probably one of the most common directives in an .htaccess file is one preventing “directory indexes.” A directory index occurs when a website visitor types in the URL of a directory on a website, and that directory also has no index file. Without anything to send out, the Apache web server will then send the visitor an index, or itemized listing, of all the files in that directory. In some cases, this can represent a security risk, and is generally just bad form. A developer can put a simple directive into a text file that will stop that from happening. Simply save the file as “.htaccess” and place it in the directory of choice, and Apache will no longer allow visitors to view a directory index.

Another common use of .htaccess files is to define custom error pages the web server should send out if it come across an error. All of us have seen that generic “Page Not Found” error, which is sent by Apache when a request is made for a file that cannot be found on the server. Again, it is often ideal to have a custom error page sent to visitors. Just like with directory indexes, there are simple directives to put in an .htaccess file that will serve out a custom page for each error defined. In this case, the .htaccess file should be in the root directory of the website, so that the directives apply to the entire site.

While these are just a couple of common examples, the greater lesson is that every web developer should be intimately familiar with .htaccess files and what they can do with them.

Brian Getting
Brian Getting
Bio   •   RSS Feed


x