]> git.ipfire.org Git - thirdparty/apache/httpd.git/commitdiff
docs: Modernize htaccess tutorial
authorRich Bowen <rbowen@apache.org>
Tue, 5 May 2026 18:22:48 +0000 (18:22 +0000)
committerRich Bowen <rbowen@apache.org>
Tue, 5 May 2026 18:22:48 +0000 (18:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1933862 13f79535-47bb-0310-9956-ffa450edef68

docs/manual/howto/htaccess.xml

index fcdcea98bb86f45891bdfeb7d1288026be8046f6..6fd286af21cf80c211ee1af2be5fe0b5e9c274c9 100644 (file)
@@ -27,7 +27,8 @@
 
 <summary>
 <p><code>.htaccess</code> files provide a way to make configuration
-changes on a per-directory basis.</p>
+changes on a per-directory basis, without modifying the main server 
+configuration files directly.</p>
 </summary>
 
 <section id="related"><title>.htaccess files</title>
@@ -36,14 +37,15 @@ changes on a per-directory basis.</p>
             <module>core</module>
             <module>mod_authn_file</module>
             <module>mod_authz_groupfile</module>
-            <module>mod_cgi</module>
             <module>mod_include</module>
             <module>mod_mime</module>
+            <module>mod_rewrite</module>
         </modulelist>
 
         <directivelist>
             <directive module="core">AccessFileName</directive>
             <directive module="core">AllowOverride</directive>
+            <directive module="core">AllowOverrideList</directive>
             <directive module="core">Options</directive>
             <directive module="mod_mime">AddHandler</directive>
             <directive module="core">SetHandler</directive>
@@ -56,9 +58,6 @@ changes on a per-directory basis.</p>
 
     </related>
 
-    <note>You should avoid using <code>.htaccess</code> files completely if you have access to
-    httpd main server config file. Using <code>.htaccess</code> files slows down your Apache http server.
-    Any directive that you can include in a <code>.htaccess</code> file is better set in a <directive module="core">Directory</directive> block, as it will have the same effect with better performance.</note>
 </section>
 
 <section id="what">
@@ -82,14 +81,17 @@ AccessFileName ".config"
       </highlight>
     </note>
 
-    <p>In general, <code>.htaccess</code> files use the same syntax as
-    the <a href="../configuring.html#syntax">main configuration
-    files</a>. What you can put in these files is determined by the
-    <directive module="core">AllowOverride</directive> directive. This
-    directive specifies, in categories, what directives will be
-    honored if they are found in a <code>.htaccess</code> file. If a
-    directive is permitted in a <code>.htaccess</code> file, the
-    documentation for that directive will contain an Override section,
+    <p>Directives in <code>.htaccess</code> files use the same syntax as
+    in the <a href="../configuring.html#syntax">main configuration
+    files</a>. What you are permitted to put in these files is determined by the
+    <directive module="core">AllowOverride</directive> and
+    <directive module="core">AllowOverrideList</directive> directives.
+    <directive module="core">AllowOverride</directive> specifies, in
+    categories, what directives will be honored if they are found in a
+    <code>.htaccess</code> file, while <directive
+    module="core">AllowOverrideList</directive> names individual
+    directives to permit (see <a href="#when">below</a>). If a directive
+    is permitted, the documentation for that directive will contain an Override section,
     specifying what value must be in <directive
     module="core">AllowOverride</directive> in order for that
     directive to be permitted.</p>
@@ -102,62 +104,34 @@ AccessFileName ".config"
     <code>FileInfo</code>. Thus, you must have at least
     <code>AllowOverride FileInfo</code> in order for this directive to be
     honored in <code>.htaccess</code> files.</p>
-
-    <example><title>Example:</title>
-      <table>
-        <tr>
-          <td><a
-          href="../mod/directive-dict.html#Context">Context:</a></td>
-          <td>server config, virtual host, directory, .htaccess</td>
-        </tr>
-
-        <tr>
-          <td><a
-          href="../mod/directive-dict.html#Override">Override:</a></td>
-          <td>FileInfo</td>
-        </tr>
-      </table>
-    </example>
-
-    <p>If you are unsure whether a particular directive is permitted in a
-    <code>.htaccess</code> file, look at the documentation for that
-    directive, and check the Context line for ".htaccess".</p>
     </section>
 
     <section id="when"><title>When (not) to use .htaccess files</title>
 
-    <p>In general, you should only use <code>.htaccess</code> files when
-    you don't have access to the main server configuration file. There is,
-    for example, a common misconception that user authentication should
-    always be done in <code>.htaccess</code> files, and, in more recent years,
-    another misconception that <module>mod_rewrite</module> directives
-    must go in <code>.htaccess</code> files. 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. Likewise, <code>mod_rewrite</code> directives work better,
-    in many respects, in the main server configuration.</p>
+    <p>If you have access to the main server configuration file, you
+    should put all of your configuration there instead of in
+    <code>.htaccess</code> files. This includes user authentication,
+    <module>mod_rewrite</module> rules, and anything else you might be
+    tempted to put in <code>.htaccess</code>. Directives in the main
+    configuration are loaded once at server start, rather than on every
+    request, and <module>mod_rewrite</module> in particular works better
+    in server configuration context.</p>
 
     <p><code>.htaccess</code> 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.
-    In the event that the server administrator is not willing to make
-    frequent configuration changes, it might be desirable to permit
+    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 <code>.htaccess</code> 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 <code>.htaccess</code> files
-    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.</p>
-
-    <p>However, in general, use of <code>.htaccess</code> files should be
-    avoided when possible. Any configuration that you would consider
-    putting in a <code>.htaccess</code> file, can just as effectively be
-    made in a <directive module="core"
-    type="section">Directory</directive> section in your main server
-    configuration file.</p>
+    for themselves.</p>
 
-    <p>There are two main reasons to avoid the use of
-    <code>.htaccess</code> files.</p>
+    <p>There are two reasons to prefer the main configuration file
+    over <code>.htaccess</code>: performance and security.</p>
 
-    <p>The first of these is performance. When <directive
+    <p><strong>Performance:</strong> When <directive
     module="core">AllowOverride</directive>
     is set to allow the use of <code>.htaccess</code> files, httpd will
     look in every directory for <code>.htaccess</code> files. Thus,
@@ -173,12 +147,12 @@ AccessFileName ".config"
     directory <code>/www/htdocs/example</code>, httpd must look for the
     following files:</p>
 
-    <example>
-      /.htaccess<br />
-      /www/.htaccess<br />
-      /www/htdocs/.htaccess<br />
-      /www/htdocs/example/.htaccess
-    </example>
+    <highlight language="config">
+/.htaccess
+/www/.htaccess
+/www/htdocs/.htaccess
+/www/htdocs/example/.htaccess
+    </highlight>
 
     <p>And so, for each file access out of that directory, there are 4
     additional file-system accesses, even if none of those files are
@@ -186,18 +160,7 @@ AccessFileName ".config"
     <code>.htaccess</code> files were enabled for <code>/</code>, which
     is not usually the case.)</p>
 
-    <p>In the case of <directive
-    module="mod_rewrite">RewriteRule</directive> directives, in
-    <code>.htaccess</code> context these regular expressions must be
-    re-compiled with every request to the directory, whereas in main
-    server configuration context they are compiled once and cached.
-    Additionally, the rules themselves are more complicated, as one must
-    work around the restrictions that come with per-directory context
-    and <code>mod_rewrite</code>. Consult the <a
-    href="../rewrite/intro.html#htaccess">Rewrite Guide</a> for more
-    detail on this subject.</p>
-
-    <p>The second consideration is one of security. You are permitting
+    <p><strong>Security:</strong> 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
@@ -208,6 +171,24 @@ AccessFileName ".config"
     to the relevant documentation, will save yourself a lot of confusion
     later.</p>
 
+    <p>If you need to grant <code>.htaccess</code> access but want to
+    limit it to specific directives rather than entire categories, use the
+    <directive module="core">AllowOverrideList</directive> directive. This
+    lets you name individual directives that are permitted, providing
+    finer-grained control than <directive
+    module="core">AllowOverride</directive> alone:</p>
+
+    <highlight language="config">
+# Allow only specific directives, not entire categories
+AllowOverride None
+AllowOverrideList Redirect RedirectMatch RewriteEngine RewriteRule RewriteCond
+    </highlight>
+
+    <p>With this configuration, any directive not explicitly listed will
+    cause a server error if encountered in a <code>.htaccess</code> file.
+    This is a useful middle ground between full override access and no
+    override access.</p>
+
     <p>Note that it is completely equivalent to put a <code>.htaccess</code>
     file in a directory <code>/www/htdocs/example</code> containing a
     directive, and to put that same directive in a Directory section
@@ -232,11 +213,6 @@ AddType text/example ".exm"
     </highlight>
     </example>
 
-    <p>However, putting this configuration in your server configuration
-    file will result in less of a performance hit, as the configuration is
-    loaded once when httpd starts, rather than every time a file is
-    requested.</p>
-
     <p>The use of <code>.htaccess</code> files can be disabled completely
     by setting the <directive module="core">AllowOverride</directive>
     directive to <code>none</code>:</p>
@@ -319,21 +295,11 @@ Options Includes
 
 <section id="auth"><title>Authentication example</title>
 
-    <p>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
-    <code>.htaccess</code> files in order to implement password
-    authentication. This is not the case. Putting authentication directives
-    in a <directive module="core" type="section">Directory</directive>
-    section, in your main server configuration file, is the preferred way
-    to implement this, and <code>.htaccess</code> files should be used only
-    if you don't have access to the main server configuration file. See <a
-    href="#when">above</a> for a discussion of when you should and should
-    not use <code>.htaccess</code> files.</p>
-
-    <p>Having said that, if you still think you need to use a
-    <code>.htaccess</code> file, you may find that a configuration such as
-    what follows may work for you.</p>
+    <p>As with any <code>.htaccess</code> use, placing these directives in
+    a <directive module="core" type="section">Directory</directive> block is
+    preferred when you have access to the main configuration (see
+    <a href="#when">above</a>). The following example shows the
+    <code>.htaccess</code> approach:</p>
 
     <p><code>.htaccess</code> file contents:</p>
 
@@ -354,10 +320,10 @@ Require group admins
 
 <section id="ssi"><title>Server Side Includes example</title>
 
-    <p>Another common use of <code>.htaccess</code> files is to enable
-    Server Side Includes for a particular directory. This may be done with
-    the following configuration directives, placed in a
-    <code>.htaccess</code> file in the desired directory:</p>
+    <p>Another use of <code>.htaccess</code> files is to enable Server Side
+    Includes for a particular directory. This may be done with the following
+    configuration directives, placed in a <code>.htaccess</code> file in
+    the desired directory:</p>
 
     <highlight language="config">
 Options +Includes
@@ -398,20 +364,30 @@ module="mod_rewrite">RewriteRule</directive>, and in the
 it. Thus, your regular expression needs to omit that portion as
 well.</p>
 
+<p>Also note that in <code>.htaccess</code> context, regular expressions are
+recompiled on every request, whereas in the main server configuration they
+are compiled once and cached.</p>
+
 <p>Consult the <a href="../rewrite/">mod_rewrite documentation</a> for
-further details on using <code>mod_rewrite</code>.</p>
+further details on using <module>mod_rewrite</module>.</p>
 
 </section>
 
 <section id="cgi"><title>CGI example</title>
 
-    <p>Finally, you may wish to use a <code>.htaccess</code> file to permit
-    the execution of CGI programs in a particular directory. This may be
+    <note>CGI scripts are a legacy mechanism for dynamic content. For new
+    deployments, consider using <module>mod_proxy_fcgi</module> with a
+    FastCGI application server, or a framework-specific handler. The
+    information below remains useful for environments that still rely on
+    traditional CGI.</note>
+
+    <p>You may wish to use a <code>.htaccess</code> file to permit the
+    execution of CGI programs in a particular directory. This may be
     implemented with the following configuration:</p>
 
     <highlight language="config">
 Options +ExecCGI
-AddHandler cgi-script "cgi" "pl"
+AddHandler cgi-script "cgi" "py"
     </highlight>
 
     <p>Alternately, if you wish to have all files in the given directory be
@@ -442,8 +418,15 @@ SetHandler cgi-script
     module="core">AllowOverride</directive> is not
     set such that your configuration directives are being honored. Make
     sure that you don't have a <code>AllowOverride None</code> in effect
-    for the file scope in question. A good test for this is to put garbage
-    in your <code>.htaccess</code> file and reload the page. If a server error is
+    for the file scope in question. A good test for this is to put a
+    nonsense word in your <code>.htaccess</code> file and reload the
+    page:</p>
+
+    <highlight language="config">
+TestMe
+    </highlight>
+
+    <p>If a server error (HTTP 500) is
     not generated, then you almost certainly have <code>AllowOverride
     None</code> in effect.</p>
 
@@ -452,9 +435,9 @@ SetHandler cgi-script
     that the directive used in your <code>.htaccess</code> file is not
     permitted.</p>
 
-    <example>
-    [Fri Sep 17 18:43:16 2010] [alert] [client 192.168.200.51] /var/www/html/.htaccess: DirectoryIndex not allowed here
-    </example>
+    <highlight language="config">
+[Tue May 06 09:12:31.528374 2025] [core:alert] [pid 12345] [client 192.168.1.50:54321] /var/www/html/.htaccess: DirectoryIndex not allowed here
+    </highlight>
 
     <p>This will indicate either that you've used a directive that is
     never permitted in <code>.htaccess</code> files, or that you simply
@@ -466,9 +449,9 @@ SetHandler cgi-script
     <p>Alternately, it may tell you that you had a syntax error in your
     usage of the directive itself.</p>
 
-    <example>
-    [Sat Aug 09 16:22:34 2008] [alert] [client 192.168.200.51] /var/www/html/.htaccess: RewriteCond: bad flag delimiters
-    </example>
+    <highlight language="config">
+[Tue May 06 09:14:02.946218 2025] [core:alert] [pid 12345] [client 192.168.1.50:54321] /var/www/html/.htaccess: RewriteCond: bad flag delimiters
+    </highlight>
 
     <p>In this case, the error message should be specific to the
     particular syntax error that you have committed.</p>