CentOS5でのindex.phpとindex.htmlのDirectoryIndex評価順序
httpd.confのDirectoryIndexを変更しても、index.htmlとindex.phpの評価順序が変更されなかったので評価順序を調べてみました。バーチャルホストで運用している際に、評価順序としては、以下のようになります。
- VirtualHost ディレクティブ内で定義した、Directoryディレクティブ内の、DirectoryIndex
- /etc/httpd/conf.d/php.conf に記述されたDirectoryIndex
- /etc/httpd/conf/httpd.conf に記述されたサーバデフォルトのDirectoryIndex
CentOS5でphp環境をyumでインストールした場合、以下のような/etc/httpd/conf.d/php.confが追加されます。この設定ファイルにより、PHP5のモジュールがロードされ、Apacheはphp拡張子を理解できるようになります。また、URLでディレクトリアクセスした場合には、DirectoryIndexで定義された、index.phpを確認し、ある場合はindex.phpを実行し、無い場合は、HTTPステータス404をクライアントに返却するようになります。
# # PHP is an HTML-embedded scripting language which attempts to make it # easy for developers to write dynamically generated webpages. # LoadModule php5_module modules/libphp5.so # # Cause the PHP interpreter to handle files with a .php extension. # AddHandler php5-script .php AddType text/html .php # # Add index.php to the list of files that will be served as directory # indexes. # DirectoryIndex index.php # # Uncomment the following line to allow PHP to pretty-print .phps # files as PHP source code: # #AddType application/x-httpd-php-source .phps
そのため、以下のようにhttpd.conf のDirectoryIndexの最後にindex.phpを追加して、html拡張子を先に評価しようとしても、
DirectoryIndex index.html index.html.var index.php
実際には、php.conf側のDirectoryIndexが先に評価されるため、以下のような定義と同様になるようです。
DirectoryIndex index.php index.html index.html.var index.php
CentOS5の場合は、index.htmlとindex.phpの評価順序を変更したい場合は、httpd.confのDirectoryIndexにindex.phpを追加するのではなく、php.conf側で以下のように指定したほうがよいのかもしれません。
DirectoryIndex index.html index.php
