mod_rewrite规则和内容协商

发布于 2024-07-12 07:53:34 字数 2534 浏览 6 评论 0原文

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

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

I am relatively new to mod_rewrite, but have a site which I would like to have "pretty urls." Similarly to SO :).

I am attempting to have things like: "http://www.whatever.com/search/test" get rewritten to "http://www.whatever.com/search.php?q=test" and have had some limited success. I believe that content negotiation is getting in my way...

For starters here's my test .htaccess file:

RewriteEngine on
RewriteBase /~user/mysite/

RewriteRule ^search$ search/ [R]
RewriteRule ^search/([^/]*)/?$ search.php?q=$1 [L]

Which unfortunately, does redirect to search.php, but does not pass my param in the q variable. However this does work:

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

In fact, if I remove the rules all together, I get the same result as with the first version of the file. So my conclusion is that since apache is happily redirecting "foo" to "foo.php" even without any mod_rewrite rules, that it must be the content negotiation that is taking care of it. (This is further verified by the fact if I renamed my foo.php to foo.html, it still will find the file if i just go to "foo").

So, the question is. How do I properly use mod_rewrite with regard to content negotiation? Can I disable it for a particular file? Is there a way to ensure that my mod_rewrite rules happen before the content negotiation happens?

If it is relevant, here is the conf file for the mod_userdir part of my apache conf (this test site is in my user's 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朕就是辣么酷 2024-07-19 07:53:34

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

Options +Multiviews

来查找

/foo/test
/foo/
/foo

并重定向它们

/foo.[any file]

它将根据其是否存在以及是否符合请求的内容类型

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

Options -Multiviews

Look for this option in your configuration.

Options +Multiviews

It will look for

/foo/test
/foo/
/foo

and redirect them to

/foo.[any file]

based upon if it exists and if it fits the content-type requested.

Change the option to this to disable this.

Options -Multiviews
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文