私は mod_rewrite に比較的慣れていませんが、「きれいな URL」が必要なサイトがあります。SOと同様:)。
「 http://www.whatever.com/search/test 」が「http://www.whatever.com/search.php?q=test 」に書き換えられ、いくつかの制限がありました成功。コンテンツの交渉が邪魔になっていると思います...
手始めに、私のテスト .htaccess ファイルを次に示します。
RewriteEngine on
RewriteBase /~user/mysite/
RewriteRule ^search$ search/ [R]
RewriteRule ^search/([^/]*)/?$ search.php?q=$1 [L]
残念ながら、これはsearch.php にリダイレクトしますが、q 変数に私のパラメーターを渡しません。ただし、これは機能します。
RewriteEngine on
RewriteBase /~user/mysite/
RewriteRule ^search$ search/ [R]
RewriteRule ^search/([^/]*)/?$ s.php?q=$1 [L] # here i've renamed the search.php to s.php to dodge the content negotiation that is happening..
実際、ルールをまとめて削除すると、ファイルの最初のバージョンと同じ結果になります。したがって、私の結論は、Apache は mod_rewrite ルールがなくても "foo" を "foo.php" に喜んでリダイレクトしているので、それを処理しているのはコンテンツ ネゴシエーションであるに違いないということです。(これは、foo.php の名前を foo.html に変更した場合でも、「foo」に移動するだけでファイルが見つかるという事実によってさらに検証されます)。
それで、質問はです。コンテンツ ネゴシエーションに関して mod_rewrite を適切に使用するにはどうすればよいですか? 特定のファイルに対して無効にすることはできますか? コンテンツ ネゴシエーションが発生する前に mod_rewrite ルールが発生するようにする方法はありますか?
関連する場合、これは私のApache confのmod_userdir部分のconfファイルです(このテストサイトは私のユーザーのhomedir/public_htmlにあります):
# Settings for user home directories
<IfDefine USERDIR>
<IfModule userdir_module>
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received. Note that you must also set
# the default access control for these directories, as in the example below.
UserDir public_html
# Control access to UserDir directories. The following is an example
# for a site where these directories are restricted to read-only.
<Directory /home/*/public_html>
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
<Limit GET POST OPTIONS>
Order allow,deny
Allow from all
</Limit>
<LimitExcept GET POST OPTIONS>
Order deny,allow
Deny from all
</LimitExcept>
</Directory>
# Suexec isn't really required to run cgi-scripts, but it's a really good
# idea if you have multiple users serving websites...
<IfDefine SUEXEC>
<IfModule suexec_module>
<Directory /home/*/public_html/cgi-bin>
Options ExecCGI
SetHandler cgi-script
</Directory>
</IfModule>
</IfDefine>
</IfModule>
</IfDefine>