Apache HTTP Server Version 2.0
Apache Tutorial: .htaccess files
.htaccessfiles provide a way to make configuration changes on a per-directory basis.
- .htaccess files
- What they are/How to use them
- When (not) to use .htaccess files
- How directives are applied
- Authentication example
- Server side includes example
- CGI example
- Troubleshooting
.htaccess files
Related Modules coremod_authmod_cgimod_includemod_mimeRelated Directives AccessFileNameAllowOverrideOptionsAddHandlerSetHandlerAuthTypeAuthNameAuthUserFileAuthGroupFileRequireWhat they are/How to use them
.htaccessfiles (or "distributed configuration files") provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.
Note: If you want to call your
.htaccessfile something else, you can change the name of the file using theAccessFileNamedirective. For example, if you would rather call the file.configthen you can put the following in your server configuration file:
AccessFileName .configWhat you can put in these files is determined by the
AllowOverridedirective. This directive specifies, in categories, what directives will be honored if they are found in a.htaccessfile. If a directive is permitted in a.htaccessfile, the documentation for that directive will contain an Override section, specifying what value must be inAllowOverridein order for that directive to be permitted.For example, if you look at the documentation for the
AddDefaultCharsetdirective, you will find that it is permitted in.htaccessfiles. (See the Context line in the directive summary.) The Override line reads "FileInfo". Thus, you must have at least "AllowOverride FileInfo" in order for this directive to be honored in.htaccessfiles.
Example:
Context: server config, virtual host, directory, .htaccess Override: FileInfo If you are unsure whether a particular directive is permitted in a
.htaccessfile, look at the documentation for that directive, and check the Context line for ".htaccess."When (not) to use .htaccess files
In general, you should never use
.htaccessfiles unless you don't have access to the main server configuration file. There is, for example, a prevailing misconception that user authentication should always be done in.htaccessfiles. This is simply not the case. You can put user authentication configurations in the main server configuration, and this is, in fact, the preferred way to do things.
.htaccessfiles should be used in a case where the content providers need to make configuration changes to the server on a per-directory basis, but do not have root access on the server system. In the event that the server administrator is not willing to make frequent configuration changes, it might be desirable to permit individual users to make these changes in.htaccessfiles for themselves. This is particularly true, for example, in cases where ISPs are hosting multiple user sites on a single machine, and want their users to be able to alter their configuration.However, in general, use of
.htaccessfiles should be avoided when possible. Any configuration that you would consider putting in a.htaccessfile, can just as effectively be made in a<Directory>section in your main server configuration file.There are two main reasons to avoid the use of
.htaccessfiles.The first of these is performance. When
AllowOverrideis set to allow the use of.htaccessfiles, Apache will look in every directory for.htaccessfiles. Thus, permitting.htaccessfiles causes a performance hit, whether or not you actually even use them! Also, the.htaccessfile is loaded every time a document is requested.Further note that Apache must look for
.htaccessfiles in all higher-level directories, in order to have a full complement of directives that it must apply. (See section on how directives are applied.) Thus, if a file is requested out of a directory/www/htdocs/example, Apache must look for the following files:
/.htaccess
/www/.htaccess
/www/htdocs/.htaccess
/www/htdocs/example/.htaccessAnd so, for each file access out of that directory, there are 4 additional file-system accesses, even if none of those files are present. (Note that this would only be the case if .htaccess files were enabled for /, which is not usually the case.)
The second consideration is one of security. You are permitting users to modify server configuration, which may result in changes over which you have no control. Carefully consider whether you want to give your users this privilege. Note also that giving users less privileges than they need will lead to additional technical support requests. Make sure you clearly tell your users what level of privileges you have given them. Specifying exactly what you have set
AllowOverrideto, and pointing them to the relevant documentation, will save yourself a lot of confusion later.Note that it is completely equivalent to put a .htaccess file in a directory
/www/htdocs/examplecontaining a directive, and to put that same directive in a Directory section<Directory /www/htdocs/example>in your main server configuration:
.htaccessfile in/www/htdocs/example:
Contents of .htaccess file in
/www/htdocs/exampleAddType text/example .exm
Section from your
httpd.conffile<Directory /www/htdocs/example>
AddType text/example .exm
</Directory>However, putting this configuration in your server configuration file will result in less of a performance hit, as the configuration is loaded once when Apache starts, rather than every time a file is requested.
The use of
.htaccessfiles can be disabled completely by setting theAllowOverridedirective to "none"
AllowOverride NoneHow directives are applied
The configuration directives found in a
.htaccessfile are applied to the directory in which the.htaccessfile is found, and to all subdirectories thereof. However, it is important to also remember that there may have been.htaccessfiles in directories higher up. Directives are applied in the order that they are found. Therefore, a.htaccessfile in a particular directory may override directives found in.htaccessfiles found higher up in the directory tree. And those, in turn, may have overridden directives found yet higher up, or in the main server configuration file itself.Example:
In the directory
/www/htdocs/example1we have a.htaccessfile containing the following:
Options +ExecCGI(Note: you must have "
AllowOverride Options" in effect to permit the use of the "Options" directive in.htaccessfiles.)In the directory
/www/htdocs/example1/example2we have a.htaccessfile containing:
Options IncludesBecause of this second
.htaccessfile, in the directory/www/htdocs/example1/example2, CGI execution is not permitted, as onlyOptions Includesis in effect, which completely overrides any earlier setting that may have been in place.Authentication example
If you jumped directly to this part of the document to find out how to do authentication, it is important to note one thing. There is a common misconception that you are required to use
.htaccessfiles in order to implement password authentication. This is not the case. Putting authentication directives in a<Directory>section, in your main server configuration file, is the preferred way to implement this, and.htaccessfiles should be used only if you don't have access to the main server configuration file. See above for a discussion of when you should and should not use.htaccessfiles.Having said that, if you still think you need to use a
.htaccessfile, you may find that a configuration such as what follows may work for you.You must have "
AllowOverride AuthConfig" in effect for these directives to be honored.
.htaccessfile contents:
AuthType Basic
AuthName "Password Required"
AuthUserFile /www/passwords/password.file
AuthGroupFile /www/passwords/group.file
Require Group adminsNote that
AllowOverride AuthConfigmust be in effect for these directives to have any effect.Please see the authentication tutorial for a more complete discussion of authentication and authorization.
Server side includes example
Another common use of
.htaccessfiles is to enable Server Side Includes for a particular directory. This may be done with the following configuration directives, placed in a.htaccessfile in the desired directory:
Options +Includes
AddType text/html shtml
AddHandler server-parsed shtmlNote that
AllowOverride OptionsandAllowOverride FileInfomust both be in effect for these directives to have any effect.Please see the SSI tutorial for a more complete discussion of server-side includes.
CGI example
Finally, you may wish to use a
.htaccessfile to permit the execution of CGI programs in a particular directory. This may be implemented with the following configuration:
Options +ExecCGI
AddHandler cgi-script cgi plAlternately, if you wish to have all files in the given directory be considered to be CGI programs, this may be done with the following configuration:
Options +ExecCGI
SetHandler cgi-scriptNote that
AllowOverride Optionsmust be in effect for these directives to have any effect.Please see the CGI tutorial for a more complete discussion of CGI programming and configuration.
Troubleshooting
When you put configuration directives in a
.htaccessfile, and you don't get the desired effect, there are a number of things that may be going wrong.Most commonly, the problem is that
AllowOverrideis not set such that your configuration directives are being honored. Make sure that you don't have aAllowOverride Nonein effect for the file scope in question. A good test for this is to put garbage in your.htaccessfile and reload. If a server error is not generated, then you almost certainly haveAllowOverride Nonein effect.If, on the other hand, you are getting server errors when trying to access documents, check your Apache error log. It will likely tell you that the directive used in your .htaccess file is not permitted. Alternately, it may tell you that you had a syntax error, which you will then need to fix.

