Skip to content

Commit e5b3877

Browse files
committed
docs: Modernize htaccess tutorial
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1933862 13f79535-47bb-0310-9956-ffa450edef68
1 parent 943faf5 commit e5b3877

1 file changed

Lines changed: 95 additions & 112 deletions

File tree

docs/manual/howto/htaccess.xml

Lines changed: 95 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
<summary>
2929
<p><code>.htaccess</code> files provide a way to make configuration
30-
changes on a per-directory basis.</p>
30+
changes on a per-directory basis, without modifying the main server
31+
configuration files directly.</p>
3132
</summary>
3233

3334
<section id="related"><title>.htaccess files</title>
@@ -36,14 +37,15 @@ changes on a per-directory basis.</p>
3637
<module>core</module>
3738
<module>mod_authn_file</module>
3839
<module>mod_authz_groupfile</module>
39-
<module>mod_cgi</module>
4040
<module>mod_include</module>
4141
<module>mod_mime</module>
42+
<module>mod_rewrite</module>
4243
</modulelist>
4344

4445
<directivelist>
4546
<directive module="core">AccessFileName</directive>
4647
<directive module="core">AllowOverride</directive>
48+
<directive module="core">AllowOverrideList</directive>
4749
<directive module="core">Options</directive>
4850
<directive module="mod_mime">AddHandler</directive>
4951
<directive module="core">SetHandler</directive>
@@ -56,9 +58,6 @@ changes on a per-directory basis.</p>
5658

5759
</related>
5860

59-
<note>You should avoid using <code>.htaccess</code> files completely if you have access to
60-
httpd main server config file. Using <code>.htaccess</code> files slows down your Apache http server.
61-
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>
6261
</section>
6362

6463
<section id="what">
@@ -82,14 +81,17 @@ AccessFileName ".config"
8281
</highlight>
8382
</note>
8483

85-
<p>In general, <code>.htaccess</code> files use the same syntax as
86-
the <a href="../configuring.html#syntax">main configuration
87-
files</a>. What you can put in these files is determined by the
88-
<directive module="core">AllowOverride</directive> directive. This
89-
directive specifies, in categories, what directives will be
90-
honored if they are found in a <code>.htaccess</code> file. If a
91-
directive is permitted in a <code>.htaccess</code> file, the
92-
documentation for that directive will contain an Override section,
84+
<p>Directives in <code>.htaccess</code> files use the same syntax as
85+
in the <a href="../configuring.html#syntax">main configuration
86+
files</a>. What you are permitted to put in these files is determined by the
87+
<directive module="core">AllowOverride</directive> and
88+
<directive module="core">AllowOverrideList</directive> directives.
89+
<directive module="core">AllowOverride</directive> specifies, in
90+
categories, what directives will be honored if they are found in a
91+
<code>.htaccess</code> file, while <directive
92+
module="core">AllowOverrideList</directive> names individual
93+
directives to permit (see <a href="#when">below</a>). If a directive
94+
is permitted, the documentation for that directive will contain an Override section,
9395
specifying what value must be in <directive
9496
module="core">AllowOverride</directive> in order for that
9597
directive to be permitted.</p>
@@ -102,62 +104,34 @@ AccessFileName ".config"
102104
<code>FileInfo</code>. Thus, you must have at least
103105
<code>AllowOverride FileInfo</code> in order for this directive to be
104106
honored in <code>.htaccess</code> files.</p>
105-
106-
<example><title>Example:</title>
107-
<table>
108-
<tr>
109-
<td><a
110-
href="../mod/directive-dict.html#Context">Context:</a></td>
111-
<td>server config, virtual host, directory, .htaccess</td>
112-
</tr>
113-
114-
<tr>
115-
<td><a
116-
href="../mod/directive-dict.html#Override">Override:</a></td>
117-
<td>FileInfo</td>
118-
</tr>
119-
</table>
120-
</example>
121-
122-
<p>If you are unsure whether a particular directive is permitted in a
123-
<code>.htaccess</code> file, look at the documentation for that
124-
directive, and check the Context line for ".htaccess".</p>
125107
</section>
126108

127109
<section id="when"><title>When (not) to use .htaccess files</title>
128110

129-
<p>In general, you should only use <code>.htaccess</code> files when
130-
you don't have access to the main server configuration file. There is,
131-
for example, a common misconception that user authentication should
132-
always be done in <code>.htaccess</code> files, and, in more recent years,
133-
another misconception that <module>mod_rewrite</module> directives
134-
must go in <code>.htaccess</code> files. This is simply not the
135-
case. You can put user authentication configurations in the main server
136-
configuration, and this is, in fact, the preferred way to do
137-
things. Likewise, <code>mod_rewrite</code> directives work better,
138-
in many respects, in the main server configuration.</p>
111+
<p>If you have access to the main server configuration file, you
112+
should put all of your configuration there instead of in
113+
<code>.htaccess</code> files. This includes user authentication,
114+
<module>mod_rewrite</module> rules, and anything else you might be
115+
tempted to put in <code>.htaccess</code>. Directives in the main
116+
configuration are loaded once at server start, rather than on every
117+
request, and <module>mod_rewrite</module> in particular works better
118+
in server configuration context.</p>
139119

140120
<p><code>.htaccess</code> files should be used in a case where the
141121
content providers need to make configuration changes to the server on a
142122
per-directory basis, but do not have root access on the server system.
143-
In the event that the server administrator is not willing to make
144-
frequent configuration changes, it might be desirable to permit
123+
This is common with managed hosting environments, control-panel-based
124+
hosting (such as cPanel or Plesk), and content management systems where
125+
the application ships a <code>.htaccess</code> file as part of its
126+
distribution. In the event that the server administrator is not willing
127+
to make frequent configuration changes, it might be desirable to permit
145128
individual users to make these changes in <code>.htaccess</code> files
146-
for themselves. This is particularly true, for example, in cases where
147-
ISPs are hosting multiple user sites on a single machine, and want
148-
their users to be able to alter their configuration.</p>
149-
150-
<p>However, in general, use of <code>.htaccess</code> files should be
151-
avoided when possible. Any configuration that you would consider
152-
putting in a <code>.htaccess</code> file, can just as effectively be
153-
made in a <directive module="core"
154-
type="section">Directory</directive> section in your main server
155-
configuration file.</p>
129+
for themselves.</p>
156130

157-
<p>There are two main reasons to avoid the use of
158-
<code>.htaccess</code> files.</p>
131+
<p>There are two reasons to prefer the main configuration file
132+
over <code>.htaccess</code>: performance and security.</p>
159133

160-
<p>The first of these is performance. When <directive
134+
<p><strong>Performance:</strong> When <directive
161135
module="core">AllowOverride</directive>
162136
is set to allow the use of <code>.htaccess</code> files, httpd will
163137
look in every directory for <code>.htaccess</code> files. Thus,
@@ -173,31 +147,20 @@ AccessFileName ".config"
173147
directory <code>/www/htdocs/example</code>, httpd must look for the
174148
following files:</p>
175149

176-
<example>
177-
/.htaccess<br />
178-
/www/.htaccess<br />
179-
/www/htdocs/.htaccess<br />
180-
/www/htdocs/example/.htaccess
181-
</example>
150+
<highlight language="config">
151+
/.htaccess
152+
/www/.htaccess
153+
/www/htdocs/.htaccess
154+
/www/htdocs/example/.htaccess
155+
</highlight>
182156

183157
<p>And so, for each file access out of that directory, there are 4
184158
additional file-system accesses, even if none of those files are
185159
present. (Note that this would only be the case if
186160
<code>.htaccess</code> files were enabled for <code>/</code>, which
187161
is not usually the case.)</p>
188162

189-
<p>In the case of <directive
190-
module="mod_rewrite">RewriteRule</directive> directives, in
191-
<code>.htaccess</code> context these regular expressions must be
192-
re-compiled with every request to the directory, whereas in main
193-
server configuration context they are compiled once and cached.
194-
Additionally, the rules themselves are more complicated, as one must
195-
work around the restrictions that come with per-directory context
196-
and <code>mod_rewrite</code>. Consult the <a
197-
href="../rewrite/intro.html#htaccess">Rewrite Guide</a> for more
198-
detail on this subject.</p>
199-
200-
<p>The second consideration is one of security. You are permitting
163+
<p><strong>Security:</strong> You are permitting
201164
users to modify server configuration, which may result in changes over
202165
which you have no control. Carefully consider whether you want to give
203166
your users this privilege. Note also that giving users less
@@ -208,6 +171,24 @@ AccessFileName ".config"
208171
to the relevant documentation, will save yourself a lot of confusion
209172
later.</p>
210173

174+
<p>If you need to grant <code>.htaccess</code> access but want to
175+
limit it to specific directives rather than entire categories, use the
176+
<directive module="core">AllowOverrideList</directive> directive. This
177+
lets you name individual directives that are permitted, providing
178+
finer-grained control than <directive
179+
module="core">AllowOverride</directive> alone:</p>
180+
181+
<highlight language="config">
182+
# Allow only specific directives, not entire categories
183+
AllowOverride None
184+
AllowOverrideList Redirect RedirectMatch RewriteEngine RewriteRule RewriteCond
185+
</highlight>
186+
187+
<p>With this configuration, any directive not explicitly listed will
188+
cause a server error if encountered in a <code>.htaccess</code> file.
189+
This is a useful middle ground between full override access and no
190+
override access.</p>
191+
211192
<p>Note that it is completely equivalent to put a <code>.htaccess</code>
212193
file in a directory <code>/www/htdocs/example</code> containing a
213194
directive, and to put that same directive in a Directory section
@@ -232,11 +213,6 @@ AddType text/example ".exm"
232213
</highlight>
233214
</example>
234215

235-
<p>However, putting this configuration in your server configuration
236-
file will result in less of a performance hit, as the configuration is
237-
loaded once when httpd starts, rather than every time a file is
238-
requested.</p>
239-
240216
<p>The use of <code>.htaccess</code> files can be disabled completely
241217
by setting the <directive module="core">AllowOverride</directive>
242218
directive to <code>none</code>:</p>
@@ -319,21 +295,11 @@ Options Includes
319295

320296
<section id="auth"><title>Authentication example</title>
321297

322-
<p>If you jumped directly to this part of the document to find out how
323-
to do authentication, it is important to note one thing. There is a
324-
common misconception that you are required to use
325-
<code>.htaccess</code> files in order to implement password
326-
authentication. This is not the case. Putting authentication directives
327-
in a <directive module="core" type="section">Directory</directive>
328-
section, in your main server configuration file, is the preferred way
329-
to implement this, and <code>.htaccess</code> files should be used only
330-
if you don't have access to the main server configuration file. See <a
331-
href="#when">above</a> for a discussion of when you should and should
332-
not use <code>.htaccess</code> files.</p>
333-
334-
<p>Having said that, if you still think you need to use a
335-
<code>.htaccess</code> file, you may find that a configuration such as
336-
what follows may work for you.</p>
298+
<p>As with any <code>.htaccess</code> use, placing these directives in
299+
a <directive module="core" type="section">Directory</directive> block is
300+
preferred when you have access to the main configuration (see
301+
<a href="#when">above</a>). The following example shows the
302+
<code>.htaccess</code> approach:</p>
337303

338304
<p><code>.htaccess</code> file contents:</p>
339305

@@ -354,10 +320,10 @@ Require group admins
354320

355321
<section id="ssi"><title>Server Side Includes example</title>
356322

357-
<p>Another common use of <code>.htaccess</code> files is to enable
358-
Server Side Includes for a particular directory. This may be done with
359-
the following configuration directives, placed in a
360-
<code>.htaccess</code> file in the desired directory:</p>
323+
<p>Another use of <code>.htaccess</code> files is to enable Server Side
324+
Includes for a particular directory. This may be done with the following
325+
configuration directives, placed in a <code>.htaccess</code> file in
326+
the desired directory:</p>
361327

362328
<highlight language="config">
363329
Options +Includes
@@ -398,20 +364,30 @@ module="mod_rewrite">RewriteRule</directive>, and in the
398364
it. Thus, your regular expression needs to omit that portion as
399365
well.</p>
400366

367+
<p>Also note that in <code>.htaccess</code> context, regular expressions are
368+
recompiled on every request, whereas in the main server configuration they
369+
are compiled once and cached.</p>
370+
401371
<p>Consult the <a href="../rewrite/">mod_rewrite documentation</a> for
402-
further details on using <code>mod_rewrite</code>.</p>
372+
further details on using <module>mod_rewrite</module>.</p>
403373

404374
</section>
405375

406376
<section id="cgi"><title>CGI example</title>
407377

408-
<p>Finally, you may wish to use a <code>.htaccess</code> file to permit
409-
the execution of CGI programs in a particular directory. This may be
378+
<note>CGI scripts are a legacy mechanism for dynamic content. For new
379+
deployments, consider using <module>mod_proxy_fcgi</module> with a
380+
FastCGI application server, or a framework-specific handler. The
381+
information below remains useful for environments that still rely on
382+
traditional CGI.</note>
383+
384+
<p>You may wish to use a <code>.htaccess</code> file to permit the
385+
execution of CGI programs in a particular directory. This may be
410386
implemented with the following configuration:</p>
411387

412388
<highlight language="config">
413389
Options +ExecCGI
414-
AddHandler cgi-script "cgi" "pl"
390+
AddHandler cgi-script "cgi" "py"
415391
</highlight>
416392

417393
<p>Alternately, if you wish to have all files in the given directory be
@@ -442,8 +418,15 @@ SetHandler cgi-script
442418
module="core">AllowOverride</directive> is not
443419
set such that your configuration directives are being honored. Make
444420
sure that you don't have a <code>AllowOverride None</code> in effect
445-
for the file scope in question. A good test for this is to put garbage
446-
in your <code>.htaccess</code> file and reload the page. If a server error is
421+
for the file scope in question. A good test for this is to put a
422+
nonsense word in your <code>.htaccess</code> file and reload the
423+
page:</p>
424+
425+
<highlight language="config">
426+
TestMe
427+
</highlight>
428+
429+
<p>If a server error (HTTP 500) is
447430
not generated, then you almost certainly have <code>AllowOverride
448431
None</code> in effect.</p>
449432

@@ -452,9 +435,9 @@ SetHandler cgi-script
452435
that the directive used in your <code>.htaccess</code> file is not
453436
permitted.</p>
454437

455-
<example>
456-
[Fri Sep 17 18:43:16 2010] [alert] [client 192.168.200.51] /var/www/html/.htaccess: DirectoryIndex not allowed here
457-
</example>
438+
<highlight language="config">
439+
[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
440+
</highlight>
458441

459442
<p>This will indicate either that you've used a directive that is
460443
never permitted in <code>.htaccess</code> files, or that you simply
@@ -466,9 +449,9 @@ SetHandler cgi-script
466449
<p>Alternately, it may tell you that you had a syntax error in your
467450
usage of the directive itself.</p>
468451

469-
<example>
470-
[Sat Aug 09 16:22:34 2008] [alert] [client 192.168.200.51] /var/www/html/.htaccess: RewriteCond: bad flag delimiters
471-
</example>
452+
<highlight language="config">
453+
[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
454+
</highlight>
472455

473456
<p>In this case, the error message should be specific to the
474457
particular syntax error that you have committed.</p>

0 commit comments

Comments
 (0)