How .htaccess Files Work

Apache HTTP Server is one of the most widely-used web server applications on Linux and Unix-like operating systems. In fact, it is even used more than Microsoft’s IIS web server. One of the unique features of Apache is the ability to add directives that customize the server configuration. You can add directives at a global level within the httpd.conf or apache2.conf file, at the virtual host level within the vhost.conf file, or at the site/directory level within an .htaccess file.

You can place an .htaccess file in any directory within your root document path on the server. Once in place, any directives placed there will take precedence over the server’s global settings. This will also be helpful if you host websites for other users who may want to use custom Apache directives. In order to enable .htaccess support, you must have the following directive in your apache httpd.conf file (in CentOS it is located at: /etc/httpd/conf/httpd.conf):

AllowOverride All

From then on, any Apache directives that can use .htaccess will be available to users, and they will not need root access to Apache to apply them. This is particularly useful for web applications and custom scripts that may need to use certain Apache modules like mod_rewrite, which many content management systems use to create search engine friendly URLs.

To create an htaccess file, make an empty text file with no extension called htaccess and upload it to the server. Enter the directives you want to use, and then rename the file .htaccess. On Linux servers, the period in front of the file name will make the file hidden. Therefore, in order to see .htaccess files in directory listings, you may need to enable viewing of all files. (from SSH, the command is “ls -al”).

For more information about .htaccess files, you can read this Apache tutorial.