我对mod_rewrite比较陌生,但有一个网站,我想拥有“漂亮的网址”。类似于SO :)。mod_rewrite规则和内容协商
我试图让事情像:“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很高兴地将“foo”重定向到“foo.php”,即使没有任何mod_rewrite规则,它也必须是正在处理它的内容协商。 (如果我将foo.php重命名为foo.html,它会发现该文件,如果我只是转到“foo”),这进一步验证了这一点。
所以,问题是。如何在内容协商方面正确使用mod_rewrite?我可以为特定文件禁用它吗?有没有办法确保我的mod_rewrite规则在内容协商发生之前发生?
如果是相关的,这里是我的Apache的conf的mod_userdir部分conf文件(这个测试网站是在我的用户的主目录/的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>