【问题标题】:mod_rewrite rules and content negotiationmod_rewrite 规则和内容协商
【发布时间】:2010-10-01 11:17:07
【问题描述】:

我对 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..

事实上,如果我将所有规则一起删除,我会得到与文件的第一个版本相同的结果。所以我的结论是,即使没有任何 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>

【问题讨论】:

    标签: apache mod-rewrite content-negotiation


    【解决方案1】:

    在您的配置中查找此选项。

    Options +Multiviews
    

    它会寻找

    /foo/test
    /foo/
    /foo
    

    并将它们重定向到

    /foo.[any file]
    

    基于它是否存在以及它是否符合请求的内容类型。

    将此选项更改为禁用此功能。

    Options -Multiviews
    

    【讨论】:

      猜你喜欢
      • 2015-04-28
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      相关资源
      最近更新 更多