1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
4 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5 This file is generated from xml source: DO NOT EDIT
6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
8 <title>Apache Tutorial: .htaccess files - Apache HTTP Server</title>
9 <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10 <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11 <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
12 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
13 <body id="manual-page"><div id="page-header">
14 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
15 <p class="apache">Apache HTTP Server Version 2.0</p>
16 <img alt="" src="../images/feather.gif" /></div>
17 <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
19 <a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.0</a> > <a href="./">How-To / Tutorials</a></div><div id="page-content"><div id="preamble"><h1>Apache Tutorial: .htaccess files</h1>
21 <p><span>Available Languages: </span><a href="../en/howto/htaccess.html" title="English"> en </a> |
22 <a href="../ja/howto/htaccess.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
23 <a href="../ko/howto/htaccess.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
26 <p><code>.htaccess</code> files provide a way to make configuration
27 changes on a per-directory basis.</p>
29 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#related">.htaccess files</a></li>
30 <li><img alt="" src="../images/down.gif" /> <a href="#what">What they are/How to use them</a></li>
31 <li><img alt="" src="../images/down.gif" /> <a href="#when">When (not) to use .htaccess files</a></li>
32 <li><img alt="" src="../images/down.gif" /> <a href="#how">How directives are applied</a></li>
33 <li><img alt="" src="../images/down.gif" /> <a href="#auth">Authentication example</a></li>
34 <li><img alt="" src="../images/down.gif" /> <a href="#ssi">Server Side Includes example</a></li>
35 <li><img alt="" src="../images/down.gif" /> <a href="#cgi">CGI example</a></li>
36 <li><img alt="" src="../images/down.gif" /> <a href="#troubleshoot">Troubleshooting</a></li>
38 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
40 <h2><a name="related" id="related">.htaccess files</a></h2>
41 <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="../mod/core.html">core</a></code></li><li><code class="module"><a href="../mod/mod_auth.html">mod_auth</a></code></li><li><code class="module"><a href="../mod/mod_cgi.html">mod_cgi</a></code></li><li><code class="module"><a href="../mod/mod_include.html">mod_include</a></code></li><li><code class="module"><a href="../mod/mod_mime.html">mod_mime</a></code></li></ul></td><td><ul><li><code class="directive"><a href="../mod/core.html#accessfilename">AccessFileName</a></code></li><li><code class="directive"><a href="../mod/core.html#allowoverride">AllowOverride</a></code></li><li><code class="directive"><a href="../mod/core.html#options">Options</a></code></li><li><code class="directive"><a href="../mod/mod_mime.html#addhandler">AddHandler</a></code></li><li><code class="directive"><a href="../mod/core.html#sethandler">SetHandler</a></code></li><li><code class="directive"><a href="../mod/core.html#authtype">AuthType</a></code></li><li><code class="directive"><a href="../mod/core.html#authname">AuthName</a></code></li><li><code class="directive"><a href="../mod/mod_auth.html#authuserfile">AuthUserFile</a></code></li><li><code class="directive"><a href="../mod/mod_auth.html#authgroupfile">AuthGroupFile</a></code></li><li><code class="directive"><a href="../mod/core.html#require">Require</a></code></li></ul></td></tr></table>
42 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
44 <h2><a name="what" id="what">What they are/How to use them</a></h2>
47 <p><code>.htaccess</code> files (or "distributed configuration files")
48 provide a way to make configuration changes on a per-directory basis. A
49 file, containing one or more configuration directives, is placed in a
50 particular document directory, and the directives apply to that
51 directory, and all subdirectories thereof.</p>
53 <div class="note"><h3>Note:</h3>
54 <p>If you want to call your <code>.htaccess</code> file something
55 else, you can change the name of the file using the <code class="directive"><a href="../mod/core.html#accessfilename">AccessFileName</a></code> directive. For example,
56 if you would rather call the file <code>.config</code> then you
57 can put the following in your server configuration file:</p>
59 <div class="example"><p><code>
60 AccessFileName .config
64 <p>In general, <code>.htaccess</code> files use the same syntax as
65 the <a href="../configuring.html#syntax">main configuration
66 files</a>. What you can put in these files is determined by the
67 <code class="directive"><a href="../mod/core.html#allowoverride">AllowOverride</a></code> directive. This
68 directive specifies, in categories, what directives will be
69 honored if they are found in a <code>.htaccess</code> file. If a
70 directive is permitted in a <code>.htaccess</code> file, the
71 documentation for that directive will contain an Override section,
72 specifying what value must be in <code class="directive"><a href="../mod/core.html#allowoverride">AllowOverride</a></code> in order for that
73 directive to be permitted.</p>
75 <p>For example, if you look at the documentation for the <code class="directive"><a href="../mod/core.html#adddefaultcharset">AddDefaultCharset</a></code>
76 directive, you will find that it is permitted in <code>.htaccess</code>
77 files. (See the Context line in the directive summary.) The <a href="../mod/directive-dict.html#Context">Override</a> line reads
78 <code>FileInfo</code>. Thus, you must have at least
79 <code>AllowOverride FileInfo</code> in order for this directive to be
80 honored in <code>.htaccess</code> files.</p>
82 <div class="example"><h3>Example:</h3><table>
84 <td><a href="../mod/directive-dict.html#Context">Context:</a></td>
85 <td>server config, virtual host, directory, .htaccess</td>
89 <td><a href="../mod/directive-dict.html#Override">Override:</a></td>
94 <p>If you are unsure whether a particular directive is permitted in a
95 <code>.htaccess</code> file, look at the documentation for that
96 directive, and check the Context line for ".htaccess".</p>
97 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
99 <h2><a name="when" id="when">When (not) to use .htaccess files</a></h2>
101 <p>In general, you should never use <code>.htaccess</code> files unless
102 you don't have access to the main server configuration file. There is,
103 for example, a prevailing misconception that user authentication should
104 always be done in <code>.htaccess</code> files. This is simply not the
105 case. You can put user authentication configurations in the main server
106 configuration, and this is, in fact, the preferred way to do
109 <p><code>.htaccess</code> files should be used in a case where the
110 content providers need to make configuration changes to the server on a
111 per-directory basis, but do not have root access on the server system.
112 In the event that the server administrator is not willing to make
113 frequent configuration changes, it might be desirable to permit
114 individual users to make these changes in <code>.htaccess</code> files
115 for themselves. This is particularly true, for example, in cases where
116 ISPs are hosting multiple user sites on a single machine, and want
117 their users to be able to alter their configuration.</p>
119 <p>However, in general, use of <code>.htaccess</code> files should be
120 avoided when possible. Any configuration that you would consider
121 putting in a <code>.htaccess</code> file, can just as effectively be
122 made in a <code class="directive"><a href="../mod/core.html#directory"><Directory></a></code> section in your main server
123 configuration file.</p>
125 <p>There are two main reasons to avoid the use of
126 <code>.htaccess</code> files.</p>
128 <p>The first of these is performance. When <code class="directive"><a href="../mod/core.html#allowoverride">AllowOverride</a></code>
129 is set to allow the use of <code>.htaccess</code> files, Apache will
130 look in every directory for <code>.htaccess</code> files. Thus,
131 permitting <code>.htaccess</code> files causes a performance hit,
132 whether or not you actually even use them! Also, the
133 <code>.htaccess</code> file is loaded every time a document is
136 <p>Further note that Apache must look for <code>.htaccess</code> files
137 in all higher-level directories, in order to have a full complement of
138 directives that it must apply. (See section on <a href="#how">how
139 directives are applied</a>.) Thus, if a file is requested out of a
140 directory <code>/www/htdocs/example</code>, Apache must look for the
143 <div class="example"><p><code>
146 /www/htdocs/.htaccess<br />
147 /www/htdocs/example/.htaccess
150 <p>And so, for each file access out of that directory, there are 4
151 additional file-system accesses, even if none of those files are
152 present. (Note that this would only be the case if
153 <code>.htaccess</code> files were enabled for <code>/</code>, which
154 is not usually the case.)</p>
156 <p>The second consideration is one of security. You are permitting
157 users to modify server configuration, which may result in changes over
158 which you have no control. Carefully consider whether you want to give
159 your users this privilege. Note also that giving users less
160 privileges than they need will lead to additional technical support
161 requests. Make sure you clearly tell your users what level of
162 privileges you have given them. Specifying exactly what you have set
163 <code class="directive"><a href="../mod/core.html#allowoverride">AllowOverride</a></code> to, and pointing them
164 to the relevant documentation, will save yourself a lot of confusion
167 <p>Note that it is completely equivalent to put a <code>.htaccess</code>
168 file in a directory <code>/www/htdocs/example</code> containing a
169 directive, and to put that same directive in a Directory section
170 <code><Directory /www/htdocs/example></code> in your main server
173 <p><code>.htaccess</code> file in <code>/www/htdocs/example</code>:</p>
175 <div class="example"><h3>Contents of .htaccess file in
176 <code>/www/htdocs/example</code></h3><p><code>
177 AddType text/example .exm
180 <div class="example"><h3>Section from your <code>httpd.conf</code>
182 <Directory /www/htdocs/example><br />
183 <span class="indent">
184 AddType text/example .exm<br />
189 <p>However, putting this configuration in your server configuration
190 file will result in less of a performance hit, as the configuration is
191 loaded once when Apache starts, rather than every time a file is
194 <p>The use of <code>.htaccess</code> files can be disabled completely
195 by setting the <code class="directive"><a href="../mod/core.html#allowoverride">AllowOverride</a></code>
196 directive to <code>none</code>:</p>
198 <div class="example"><p><code>
201 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
202 <div class="section">
203 <h2><a name="how" id="how">How directives are applied</a></h2>
205 <p>The configuration directives found in a <code>.htaccess</code> file
206 are applied to the directory in which the <code>.htaccess</code> file
207 is found, and to all subdirectories thereof. However, it is important
208 to also remember that there may have been <code>.htaccess</code> files
209 in directories higher up. Directives are applied in the order that they
210 are found. Therefore, a <code>.htaccess</code> file in a particular
211 directory may override directives found in <code>.htaccess</code> files
212 found higher up in the directory tree. And those, in turn, may have
213 overridden directives found yet higher up, or in the main server
214 configuration file itself.</p>
218 <p>In the directory <code>/www/htdocs/example1</code> we have a
219 <code>.htaccess</code> file containing the following:</p>
221 <div class="example"><p><code>
225 <p>(Note: you must have "<code>AllowOverride Options</code>" in effect
226 to permit the use of the "<code class="directive"><a href="../mod/core.html#options">Options</a></code>" directive in
227 <code>.htaccess</code> files.)</p>
229 <p>In the directory <code>/www/htdocs/example1/example2</code> we have
230 a <code>.htaccess</code> file containing:</p>
232 <div class="example"><p><code>
236 <p>Because of this second <code>.htaccess</code> file, in the directory
237 <code>/www/htdocs/example1/example2</code>, CGI execution is not
238 permitted, as only <code>Options Includes</code> is in effect, which
239 completely overrides any earlier setting that may have been in
242 <h3><a name="merge" id="merge">Merging of .htaccess with the main
243 configuration files</a></h3>
245 <p>As discussed in the documentation on <a href="../sections.html">Configuration Sections</a>,
246 <code>.htaccess</code> files can override the <code class="directive"><a href="../mod/core.html#directory"><Directory></a></code> sections for
247 the corresponding directory, but will be overriden by other types
248 of configuration sections from the main configuration files. This
249 fact can be used to enforce certain configurations, even in the
250 presence of a liberal <code class="directive"><a href="../mod/core.html#allowoverride">AllowOverride</a></code> setting. For example, to
251 prevent script execution while allowing anything else to be set in
252 <code>.htaccess</code> you can use:</p>
254 <div class="example"><p><code>
255 <Directory /><br />
256 <span class="indent">
257 Allowoverride All<br />
259 </Directory><br />
261 <Location /><br />
262 <span class="indent">
263 Options +IncludesNoExec -ExecCGI<br />
269 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
270 <div class="section">
271 <h2><a name="auth" id="auth">Authentication example</a></h2>
273 <p>If you jumped directly to this part of the document to find out how
274 to do authentication, it is important to note one thing. There is a
275 common misconception that you are required to use
276 <code>.htaccess</code> files in order to implement password
277 authentication. This is not the case. Putting authentication directives
278 in a <code class="directive"><a href="../mod/core.html#directory"><Directory></a></code>
279 section, in your main server configuration file, is the preferred way
280 to implement this, and <code>.htaccess</code> files should be used only
281 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
282 not use <code>.htaccess</code> files.</p>
284 <p>Having said that, if you still think you need to use a
285 <code>.htaccess</code> file, you may find that a configuration such as
286 what follows may work for you.</p>
288 <p>You must have "<code>AllowOverride AuthConfig</code>" in effect for
289 these directives to be honored.</p>
291 <p><code>.htaccess</code> file contents:</p>
293 <div class="example"><p><code>
295 AuthName "Password Required"<br />
296 AuthUserFile /www/passwords/password.file<br />
297 AuthGroupFile /www/passwords/group.file<br />
301 <p>Note that <code>AllowOverride AuthConfig</code> must be in effect
302 for these directives to have any effect.</p>
304 <p>Please see the <a href="auth.html">authentication tutorial</a> for a
305 more complete discussion of authentication and authorization.</p>
306 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
307 <div class="section">
308 <h2><a name="ssi" id="ssi">Server Side Includes example</a></h2>
310 <p>Another common use of <code>.htaccess</code> files is to enable
311 Server Side Includes for a particular directory. This may be done with
312 the following configuration directives, placed in a
313 <code>.htaccess</code> file in the desired directory:</p>
315 <div class="example"><p><code>
316 Options +Includes<br />
317 AddType text/html shtml<br />
318 AddHandler server-parsed shtml
321 <p>Note that <code>AllowOverride Options</code> and <code>AllowOverride
322 FileInfo</code> must both be in effect for these directives to have any
325 <p>Please see the <a href="ssi.html">SSI tutorial</a> for a more
326 complete discussion of server-side includes.</p>
327 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
328 <div class="section">
329 <h2><a name="cgi" id="cgi">CGI example</a></h2>
331 <p>Finally, you may wish to use a <code>.htaccess</code> file to permit
332 the execution of CGI programs in a particular directory. This may be
333 implemented with the following configuration:</p>
335 <div class="example"><p><code>
336 Options +ExecCGI<br />
337 AddHandler cgi-script cgi pl
340 <p>Alternately, if you wish to have all files in the given directory be
341 considered to be CGI programs, this may be done with the following
344 <div class="example"><p><code>
345 Options +ExecCGI<br />
346 SetHandler cgi-script
349 <p>Note that <code>AllowOverride Options</code> and <code>AllowOverride
350 FileInfo</code> must both be in effect for these directives to have any
353 <p>Please see the <a href="cgi.html">CGI tutorial</a> for a more
354 complete discussion of CGI programming and configuration.</p>
356 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
357 <div class="section">
358 <h2><a name="troubleshoot" id="troubleshoot">Troubleshooting</a></h2>
360 <p>When you put configuration directives in a <code>.htaccess</code>
361 file, and you don't get the desired effect, there are a number of
362 things that may be going wrong.</p>
364 <p>Most commonly, the problem is that <code class="directive"><a href="../mod/core.html#allowoverride">AllowOverride</a></code> is not
365 set such that your configuration directives are being honored. Make
366 sure that you don't have a <code>AllowOverride None</code> in effect
367 for the file scope in question. A good test for this is to put garbage
368 in your <code>.htaccess</code> file and reload. If a server error is
369 not generated, then you almost certainly have <code>AllowOverride
370 None</code> in effect.</p>
372 <p>If, on the other hand, you are getting server errors when trying to
373 access documents, check your Apache error log. It will likely tell you
374 that the directive used in your <code>.htaccess</code> file is not
375 permitted. Alternately, it may tell you that you had a syntax error,
376 which you will then need to fix.</p>
379 <div class="bottomlang">
380 <p><span>Available Languages: </span><a href="../en/howto/htaccess.html" title="English"> en </a> |
381 <a href="../ja/howto/htaccess.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |
382 <a href="../ko/howto/htaccess.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
383 </div><div id="footer">
384 <p class="apache">Copyright 2009 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
385 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div>