1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.ja.xsl"?>
4 <!-- English Revision: 574882:1741842 (outdated) -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="htaccess.xml.meta">
24 <parentdocument href="./">How-To / チュートリアル</parentdocument>
26 <title>Apache チュートリアル: .htaccess ファイル</title>
29 <p><code>.htaccess</code> ファイルはディレクトリ毎に設定を変更する方法を
33 <section id="related"><title>.htaccess ファイル</title>
37 <module>mod_authn_file</module>
38 <module>mod_authz_groupfile</module>
39 <module>mod_cgi</module>
40 <module>mod_include</module>
41 <module>mod_mime</module>
45 <directive module="core">AccessFileName</directive>
46 <directive module="core">AllowOverride</directive>
47 <directive module="core">Options</directive>
48 <directive module="mod_mime">AddHandler</directive>
49 <directive module="core">SetHandler</directive>
50 <directive module="mod_authn_core">AuthType</directive>
51 <directive module="mod_authn_core">AuthName</directive>
52 <directive module="mod_authn_file">AuthUserFile</directive>
53 <directive module="mod_authz_groupfile">AuthGroupFile</directive>
54 <directive module="mod_authz_core">Require</directive>
61 <title>.htaccess ファイルとは何か/その使い方</title>
63 <p><code>.htaccess</code> ファイル (「分散設定ファイル」) は
64 ディレクトリ毎に設定を変更する方法を提供します。ディレクティブの
65 書かれたファイルをディレクトリに置くことで、そのディレクトリとその
66 サブディレクトリすべてにディレクティブを適用させることができます。</p>
68 <note><title>注:</title>
69 <p><code>.htaccess</code> ファイルを別の名前にしたい場合は、
70 <directive module="core">AccessFileName</directive> ディレクティブを
71 使って変更することができます。例えば、そのファイルを <code>.config</code>
72 という名前にしたい場合は、以下の設定をサーバ設定ファイルに入れることが
76 AccessFileName .config
80 <p>一般に、<code>.htaccess</code> ファイルの構文は
81 <a href="../configuring.html#syntax">主設定ファイル</a>
82 と同じです。これらのファイルに書くことのできるディレクティブは <directive
83 module="core">AllowOverride</directive> ディレクティブにより決まります。
84 このディレクティブは、<code>.htaccess</code> ファイルに
86 どのディレクティブが適用されるかをカテゴリー単位で指定します。
87 <code>.htaccess</code> に書くことのできるディレクティブであれば、
88 説明文書には「上書き」という項目があり、.htaccess に書くことができるように
90 module="core">AllowOverride</directive> の値が指定されています。</p>
93 module="core">AddDefaultCharset</directive> ディレクティブの説明を
94 見ると、<code>.htaccess</code> ファイルでの使用が許可されていることが
95 わかります。 (ディレクティブの概要の所にある「コンテキスト」と書かれている
97 href="../mod/directive-dict.html#Context">上書き</a>と書かれている行には
98 <code>FileInfo</code> とあります。ですから、<code>.htaccess</code> 中の
99 このディレクティブが有効になるためには、少なくとも
100 <code>AllowOverride FileInfo</code> が設定されている必要があります。</p>
102 <example><title>例:</title>
106 href="../mod/directive-dict.html#Context">コンテキスト:</a></td>
107 <td>サーバ設定ファイル,バーチャルホスト,ディレクトリ,.htaccess</td>
112 href="../mod/directive-dict.html#Override">上書き:</a></td>
118 <p>あるディレクティブを <code>.htaccess</code> ファイルに書くことができるか
119 どうかわからないときは、そのディレクティブの説明を探して、".htaccess"
120 のための「コンテキスト」の行を調べてください。</p>
123 <section id="when"><title>いつ .htaccess ファイルを使う(使わない)か。</title>
125 <p>一般的に、サーバの主設定ファイルにアクセスできない場合を除いて、
126 <code>.htaccess</code> ファイルの使用は極力避けてください。
127 世の中には、例えば、ユーザ認証は常に <code>.htaccess</code> ファイルで
128 行なわなければならない、という誤解が広まっていますが、まったくそんなことは
129 ありません。ユーザ認証の設定はサーバ主設定ファイルに書くことができ、
130 実際、その方がより良い設定方法です。</p>
132 <p><code>.htaccess</code> ファイルはコンテンツ提供者がディレクトリ毎の
133 設定を行ないたいけれど、サーバシステムの root アクセス権限を持っていない
134 という場合にのみ使うべきものです。サーバ管理者が頻繁に設定変更を行ないたくは
135 ない、というときには個々のユーザが <code>.htaccess</code> ファイルを使って
136 自分で設定の変更を行なうことを許可した方が良いときもあるでしょう。
137 これは特に、ISP が複数のユーザのサイトを一つのマシンでホストしていて、
138 各ユーザが設定の変更をできるようにしたいようなときにあてはまります。</p>
140 <p>しかし、普通は可能であれば <code>.htaccess</code> ファイルの使用は
141 避けてください。<code>.htaccess</code> ファイルに書こうと考えるような
142 すべての設定は、サーバの主設定ファイルの <directive module="core"
143 type="section">Directory</directive> セクションで同じように行なうことが
146 <p><code>.htaccess</code> ファイルの使用を避ける理由は主に二つあります。</p>
148 <p>一つ目はサーバの性能の問題です。<directive
149 module="core">AllowOverride</directive> ディレクティブが
150 <code>.htaccess</code> ファイルの設定を許可している場合は、Apache は
151 各ディレクトリで <code>.htaccess</code> ファイルを探します。
152 ですから、<code>.htaccess</code> ファイルを許可すると、実際に使用しているか
153 どうかに関わらず、性能の低下を招くことになります! また、<code>.htaccess</code>
154 ファイルは文書がリクエストされる度に読み込まれます。</p>
156 <p>さらに、Apache は適用すべきディレクティブを集めるために、すべての
157 上位のディレクトリの <code>.htaccess</code> ファイルを探す必要があることにも
158 注意してください。(<a href="#how">ディレクティブが適用される方法</a>を
159 参照してください。)ですから、<code>/www/htdocs/example</code> にある
160 ファイルがリクエストされたときは、Apache は以下のファイルを調べます。</p>
165 /www/htdocs/.htaccess<br />
166 /www/htdocs/example/.htaccess
169 <p>ですから、そのディレクトリのそれぞれのファイルへのアクセスに対して、
170 上の例のファイルがまったく存在しないときでも、追加のファイルシステムの
171 アクセスが行なわれることになります。(これは、<code>.htaccess</code> が
172 <code>/</code> に対して有効になっているときの場合で、普通はそうなって
175 <p>二つ目はセキュリティです。ユーザにサーバの設定を変更することを
176 許可することになりますので、あなた自身が管理できない変更をされる
177 恐れがあります。ユーザにこの特権を与えるのが良いのかどうか、十分
178 検討してください。また、ユーザに与える権限が必要なものよりも少なすぎると、
179 余分な技術サポート報告を受け取るようになる可能性が高いことにも
180 注意してください。確実に、ユーザにどの程度の権限を与えたか明確に告げるように
181 してください。<directive module="core">AllowOverride</directive> に
182 何を設定したかということと、関連する文書を示すことで、
186 <p>ところで、ディレクティブの書かれた <code>.htaccess</code> を
187 <code>/www/htdocs/example</code> に置くことと、同じディレクティブを
188 主サーバ設定の Directory セクション
189 <code><Directory /www/htdocs/example></code> に書くことは
192 <p><code>/www/htdocs/example</code> の <code>.htaccess</code> ファイル:</p>
194 <example><title><code>/www/htdocs/example</code> の .htaccess ファイルの
196 AddType text/example .exm
199 <example><title><code>httpd.conf のセクション</code>
201 <Directory /www/htdocs/example><br />
203 AddType text/example .exm<br />
208 <p>しかし、この設定はサーバ設定ファイルに書いた方がパフォーマンスの
209 低下が少なくなります。ファイルがリクエストされる度に
210 読み込まれる代わりに、Apache の起動時に 1 回だけ読み込めば
213 <p><directive module="core">AllowOverride</directive> ディレクティブの
214 値を <code>none</code> に設定することで <code>.htaccess</code> ファイル
215 の使用を完全に無効にすることができます。</p>
222 <section id="how"><title>ディレクティブの適用のされ方</title>
224 <p><code>.htaccess</code> ファイルの設定ディレクティブは <code>.htaccess</code>
225 ファイルの存在するディレクトリと、そのサブディレクトリすべてに適用されます。
226 しかし、上の階層のディレクトリにも <code>.htaccess</code> ファイルが
227 存在するかもしれないことを覚えておくことは大切です。ディレクティブは現れる
228 順番に適用されます。ですから、あるディレクトリの <code>.htaccess</code> は
229 ディレクトリツリーのより上の階層の <code>.htaccess</code> ファイルの
230 設定を上書きするかもしれません。そして、その <code>.htaccess</code> も
231 より上の階層で書かれたディレクティブを上書きしたり、主サーバ設定ファイル
232 そのものの設定を上書きしたりしているかもしれません。</p>
236 <p>ディレクトリ <code>/www/htdocs/example1</code> に以下の内容の
237 <code>.htaccess</code> ファイルがあります:</p>
243 <p>(注: <code>.htaccess</code>
245 module="core">Options</directive>" ディレクティブが有効になるためには、
246 "<code>AllowOverride Options</code>" を有効にする必要があります。)</p>
248 <p>ディレクトリ <code>/www/htdocs/example1/example2</code> には
249 以下のような <code>.htaccess</code> ファイルがあります:</p>
255 <p>二つめの <code>.htaccess</code> により、ディレクトリ
256 <code>/www/htdocs/example1/example2</code> では CGI の実行は
257 許可されません。これは、<code>Options Includes</code> のみが
258 効力を持ち、それがすべての以前の設定を上書きするからです。</p>
260 <section id="merge"><title>メイン設定ファイルに対する
261 .htaccess のマージ</title>
263 <p>As discussed in the documentation on <a
264 href="../sections.html">Configuration Sections</a>,
265 <code>.htaccess</code> files can override the <directive
266 type="section" module="core">Directory</directive> sections for
267 the corresponding directory, but will be overriden by other types
268 of configuration sections from the main configuration files. This
269 fact can be used to enforce certain configurations, even in the
270 presence of a liberal <directive
271 module="core">AllowOverride</directive> setting. For example, to
272 prevent script execution while allowing anything else to be set in
273 <code>.htaccess</code> you can use:</p>
274 <p><a href="../sections.html">セクションの設定</a>
275 に記載されているように、<code>.htaccess</code> ファイルを使って
276 <directive type="section" module="core">Directory</directive>
277 セクションの設定をディレクトリ毎に上書きできますが、
278 メイン設定ファイル中にある、他の種類の設定セクションによって
281 <directive module="core">AllowOverride</directive>
282 で自由度の高い設定があったとしても、ある特定の設定が確実に
283 反映されるようにできます。例えば、CGI スクリプトの実行は
284 不許可に、かつ、<code>.htaccess</code> でその他の項目は
285 設定できるように、という場合は次のようにできます :</p>
288 <Directory /><br />
290 Allowoverride All<br />
292 </Directory><br />
294 <Location /><br />
296 Options +IncludesNoExec -ExecCGI<br />
304 <section id="auth"><title>認証の例</title>
306 <p>もし認証の方法を知るためにこの部分に直接来たのであれば、次のことを
307 知っておくことが重要です。よくある誤解に、パスワード認証を行なうためには
308 <code>.htaccess</code> ファイルを使う必要がある、というものがあります。
309 これは正しくありません。主サーバ設定ファイルの <directive
310 module="core" type="section">Directory</directive> セクションに
311 認証用のディレクティブを書く方が推奨される方法で、<code>.htaccess</code>
312 ファイルは主サーバ設定ファイルを変更できないときにのみ使用すべきです。
313 いつ <code>.htaccess</code> ファイルを使うべきで、いつ使うべきではないかに
315 href="#when">上</a>を参照してください。</p>
317 <p>以上のことをふまえた上で、もし <code>.htaccess</code> の使用が
318 まだ必要だと思う場合は、次のようなものが望みのことをしてくれるかも
321 <p><code>.htaccess</code> ファイルの内容:</p>
325 AuthName "Password Required"<br />
326 AuthUserFile /www/passwords/password.file<br />
327 AuthGroupFile /www/passwords/group.file<br />
331 <p>これらのディレクティブが有効になるためには、
332 <code>AllowOverride AuthConfig</code> が有効でなくてはならないことに
335 <p>認証と承認については <a href="auth.html">認証チュートリアル</a>を
339 <section id="ssi"><title>SSI の例</title>
341 <p>もう一つの <code>.htaccess</code> ファイルのよくある利用法は
342 特定のディレクトリで SSI を有効にすることです。これは、望みのディレクトリの
343 <code>.htaccess</code> ファイルに以下の設定ディレクティブを書くことで
347 Options +Includes<br />
348 AddType text/html shtml<br />
349 AddHandler server-parsed shtml
352 <p>これらのディレクティブが有効になるためには、
353 <code>AllowOverride Options</code> と <code>AllowOverride
354 FileInfo</code> が有効になっている必要があることに注意してください。</p>
356 <p>よりまとまった SSI の説明は <a href="ssi.html">SSI チュートリアル</a>を
360 <section id="cgi"><title>CGI の例</title>
362 <p>最後に、特定のディレクトリで CGI プログラムの実行を許可したいことが
363 あるでしょう。これは以下の設定で行なうことができます:</p>
366 Options +ExecCGI<br />
367 AddHandler cgi-script cgi pl
370 <p>もしくは、あるディレクトリのすべてのファイルが CGI プログラムと
371 みなされるようにしたいなら、以下の設定で実現することができます:</p>
374 Options +ExecCGI<br />
375 SetHandler cgi-script
378 <p>これらのディレクティブが有効になるためには、
379 <code>AllowOverride Options</code> と <code>AllowOverride
380 FileInfo</code> が有効である必要があることに注意してください。</p>
382 <p>CGI プログラムと設定のよりまとまった説明は <a
383 href="cgi.html">CGI チュートリアル</a>を参照してください。</p>
387 <section id="troubleshoot"><title>問題解決</title>
389 <p>設定ディレクティブを <code>.htaccess</code> ファイルに書いたけれども、
390 期待した効果が得られないときには、いくつかの原因が考えられます。</p>
392 <p>一番よくあることは、設定ディレクティブが考慮されるようには
393 <directive module="core">AllowOverride</directive> が設定されていない
394 というものです。該当のファイルのスコープに <code>AllowOverride None</code>
395 が設定されていないことを確認してください。これを調べるための良い方法は、
396 <code>.htaccess</code> ファイルにごみを書いて、リロードすることです。
397 サーバのエラーが生成されないときは、ほぼ確実に <code>AllowOverride
398 None</code> が設定されている状態になっています。</p>
400 <p>そうではなく、文書をアクセスしようとしたときにエラーが発生している
401 ときは、Apache のエラーログを調べてください。<code>.htaccess</code> ファイルで
402 使用されたディレクティブが許可されていない、ということを知らせている
403 可能性が高いです。または、構文の間違いがあることを述べているかもしれません。
404 その場合にはまずそれを修正する必要があります。</p>