网页 综合 更新时间 2026-11-14 05:10:42

Apache HTTP Server Tutorial: .htaccess files - Apache HTTP Server Version 2.4

自动蜘蛛 httpd.apache.org 正文约 6000 字
Modules | Directives | FAQ | Glossary | Sitemap | Report a bug
Apache HTTP Server Version 2.4
Available Languages: en | es | fr | ja | ko | pt-br
.htaccess files provide a way to make configuration changes on a per-directory basis, without modifying the main server configuration files directly.
.htaccess files (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.
If you want to call your .htaccess file something else, you can change the name of the file using the AccessFileName directive. For example, if you would rather call the file .config then you can put the following in your server configuration file:
Directives in .htaccess files use the same syntax as in the main configuration files. What you are permitted to put in these files is determined by the AllowOverride and AllowOverrideList directives. AllowOverride specifies, in categories, what directives will be honored if they are found in a .htaccess file, while AllowOverrideList names individual directives to permit (see below). If a directive is permitted, the documentation for that directive will contain an Override section, specifying what value must be in AllowOverride for that directive to be permitted.
For example, if you look at the documentation for the AddDefaultCharset directive, you will find that it is permitted in .htaccess files. (See the Context line in the directive summary.) The Override line reads FileInfo. Thus, you must have at least AllowOverride FileInfo for this directive to be honored in .htaccess files.
If you have access to the main server configuration file, you should put all of your configuration there instead of in .htaccess files. This includes user authentication, mod_rewrite rules, and anything else you might be tempted to put in .htaccess. Directives in the main configuration are loaded once at server start, rather than on every request, and mod_rewrite in particular works better in server configuration context.
.htaccess files 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. This is common with managed hosting environments, control-panel-based hosting (such as cPanel or Plesk), and content management systems where the application ships a .htaccess file as part of its distribution. 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 .htaccess files for themselves.
There are two reasons to prefer the main configuration file over .htaccess: performance and security.
Performance: When AllowOverride is set to allow the use of .htaccess files, httpd will look in every directory for .htaccess files. Thus, permitting .htaccess files causes a performance hit, whether or not you actually even use them! Also, the .htaccess file is loaded every time a document is requested.
Additionally, httpd must look for .htaccess files in all higher-level directories to assemble the full set of applicable directives. (See section on how directives are applied.) Thus, if a file is requested out of a directory /www/htdocs/example, httpd must look for the following files:
And so, for each file access out of that directory, there are 4 additional file-system accesses, even if none of those files are present. (This would only be the case if .htaccess files were enabled for /, which is not usually the case.)
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. 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 AllowOverride to, and pointing them to the relevant documentation, prevents confusion later.
If you need to grant .htaccess access but want to limit it to specific directives rather than entire categories, use the AllowOverrideList directive. This lets you name individual directives that are permitted, providing finer-grained control than AllowOverride alone:
With this configuration, any directive not explicitly listed will cause a server error if encountered in a .htaccess file. This is a useful middle ground between full override access and no override access.
Placing a directive in a .htaccess file in a directory /www/htdocs/example is completely equivalent to placing that same directive in a <Directory "/www/htdocs/example"> section in your main server configuration:
.htaccess file in /www/htdocs/example:
The use of .htaccess files can be disabled completely by setting the AllowOverride directive to none:
The configuration directives found in a .htaccess file are applied to the directory in which the .htaccess file is found, and to all subdirectories thereof. Remember that there may have been .htaccess files in directories higher up. Directives are applied in the order that they are found. Therefore, a .htaccess file in a particular directory may override directives found in .htaccess files 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.
In the directory /www/htdocs/example1 we have a .htaccess file containing the following:
In the directory /www/htdocs/example1/example2 we have a .htaccess file containing:
Because of this second .htaccess file, in the directory /www/htdocs/example1/example2, CGI execution is not permitted, as only Options Includes is in effect, which completely overrides any earlier setting that may have been i
访问原文 返回搜索 https://httpd.apache.org/docs/current/howto/htaccess.html
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。