我对 mod_rewrite 比较陌生,但是有一个我想拥有“漂亮网址”的网站。类似于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..
事实上,如果我一起删除所有规则,我会得到与文件的第一个版本相同的结果。所以我的结论是,即使没有任何 mod_rewrite 规则,apache 也很乐意将“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>