7 Powerful .htaccess Customization Tips - .htaccess Naming Conventions and File Creation
(Page 2 of 5 )
Firstly, do not let the period preceding the name intimidate you. The .htaccess name, in a technical sense, is simply a file extension. The difference here is no file name is cited.
Anyone running the Apache web server can generate and use the .htaccess file. Creating the file is as effortless as opening a text editor, like Notepad or Homesite, and saving the file as .htaccess. The file is uploaded into a particular directory on the server in ASCII mode, using permissions of 644. We choose 644 to ensure adequate security from the outside world. If an intruder seizes the .htaccess file, the webmaster of that particular web site can be in big trouble.
Keep in mind that the .htaccess file is not only interpreted in the directory you place it in, but also all directories underneath it. For example, if the .htaccess file was placed in the root directory of your site, say thecoolsite.com, other directories like thecoolsite.com/subdir and thecoolsite.com/subdir/subdir will use that file. To override the root directory's .htaccess file, simply place a blank .htaccess file within the respective directory. The directory will search for and consult the closest .htaccess file to it. So, what will this file allow us to do? Read on...
What is the .htaccess File? To fully comprehend the .htaccess file and appreciate its usefulness, we first must discuss the main Apache configuration file, called httpd.conf. The httpd.conf file, known simply as the config file, is where the Apache web server's configuration details are held, including modules, directives, port numbers and other values.. It resides on the administrator's machine.
Every time an Apache-based web site is loaded, the httpd.conf file is consulted and interpreted. Whenever the file is modified, the administrator restarts (or bounces) the web server, which will compile the httpd.conf file again with the modifications intact.
However, when web servers are built to support outside clients, like a web hosting company's web servers, administrators do not want clients to have access to the httpd.conf file. If they did, customers could inflict extreme damage on the server. However, at the same time, there are many utilities that customers should have the freedom to explore for their web site.
What is the solution? You guessed it, the .htaccess file. This file is merely an extension to the httpd.conf file; only this time, customers have access to it. Restrictions are placed on the .htaccess file to prevent customers from intentionally or inadvertently damaging default server configurations. Each time an Apache-based web page is loaded, the nearest .htaccess file is consulted and interpreted. The web server does not need to be restarted (or bounced) after each change of the .htaccess file. The file simply needs to be re-uploaded.
Since we now know a little about what the .htaccess file is and what it does, we can examine the magic of the file and look at some typical implementations.
Uses of the .htaccess file Do not fret, the fun has arrived. Here is where we take a look at actually implementing the functions that make up the .htaccess file.
Error Documents 
When an error occurs, Apache will consult the .htaccess file to determine the proper response. If no response is found in the .htaccess file or if no .htaccess file is present, it will revert to the browser's default error document page.
Before you begin consider the table to the left, which depicts the most common online status codes. Never, and I do stress never, implement 200 in the .htaccess file. If you do, you can create some extremely funky looped results.
Let us go ahead and create the error document coding for a 404 error, the most common error on the Internet, which occurs when a web page cannot be found.
ErrorDocument 404 /errorpages/404.html Looking at the code above, we can see that:
- ErrorDocument: Tells the server that this line contains an error document handler, and an error document number ensues.
- 404: The particular error document code.
- /errorpages/404.html: The page that the server should display when the error code is encountered.
In the above example, when the server cannot find a page, the .htaccess file instructs the server to display the page located at /errorpages/404.html. Notice how I began the relative link from root (/). Since this .htaccess file will be interpreted in each directory below it, we should always begin our links from the root, or the server may not find the error document page.
For other error document pages, simply use the same format with the respective error code. For example:
ErrorDocument 403 /errorpages/403.html
ErrorDocument 500 /errorpages/500.htmlNext: Modifying Directory Indexes >>
More Apache Articles
More By Steve Adcock