What is the .htaccess File, and What Can I Do With It?
Before we go any farther, let's explain what the .htaccess file is. htaccess is short for Hypertext Access, and is a configuration file used by Apache-based web servers that controls the directory that it "lives" in--as well as all the subdirectories underneath that directory.
Many times, if you have installed a Content Management System (CMS), such as Drupal, Joomla or Wordpress, you likely encountered the .htaccess file. You may not have even had to edit it, but it was among the files that you uploaded to your web server. BTW, that's its name, .htaccess--it begins with a period, and ends with "htaccess." If you edit it, you need to make sure that it stays that way, and doesn't end up with a .txt or .html extension.
Also note that some web hosts do not allow you to edit the .htaccess file--but even on most of those hosts, you can create your own .htaccess file and upload it to specific directories, and as discussed above, it will control those directories and subdirectories below it.
Custom Error Pages for Better SEOOne use of the .htaccess file is to redirect users to a custom error page depending on the specific web server error they encounter. By using a custom error page, you can present them with a list of your site's top articles, a sitemap with links to the various areas of your site, and it can include your site's navigation system. It can also feature a FAQ, so folks who are looking for information on your site, but can't find it, are able to narrow down the location of that information on your site without leaving, going back to the search engine, and more than likely not returning to your site.
It's not difficult to use the .htaccess file to redirect users to a custom error page--but to do it you'll need to know the proper error code. The most common ones you'll use are:
- 400 - Bad request
- 401 - Authorization Required
- 403 - Forbidden
- 404 - File Not Found
- 500 - Internal Server Error
ErrorDocument 404 /filenotfound.htmlIf you wanted to redirect users for another error, such as 500, Internal Server Error, you would do it like this (and so on):
ErrorDocument 500 /servererror.htmlThen you'd just save the .htaccess file (remembering to check that it is saved just like that, without some additional extension), and upload it to your web host's root directory (or whatever directory you are wanting to use it in).
Using a Different File as the Index FileBy adding another "command" to the .htaccess file, you can use a different file as the main index file that folks see when they come to your site. In other words, when folks visit www.yoursite.com, usually they are presented with www.yoursite.com/index.html or www.yoursite.com/index.php--but often you have created a special page that you want to use as your site's main page--and it isn't any of the traditional pages. Or you may want to take advantage of the latest version of PHP...there are many reasons for needing to use a different file as the index file. Here's how you would do it, with each type of file being next in line, if the others are not in the directory.
DirectoryIndex index.php3 index.php pictures.pl index.html default.htmFor instance, if there was no file named index.php3 in your directory, then the server would look for a file called index.php. If that file wasn't present, it would look for one called pictures.pl, and so on.
In our next article, we'll look at other types of redirection, password protection and we'll show you how to allow or deny users with specific IP addresses from accessing your site.