【问题标题】:How to use multiple RewriteRule rules in htaccess?如何在 htaccess 中使用多个 RewriteRule 规则?
【发布时间】:2011-09-28 22:13:46
【问题描述】:

我在我的 htaccess 中使用了 2 个 RewriteRule 规则

RewriteEngine on
RewriteBase /
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#if it has member/photo/render_fast forward to photo_render.php
RewriteRule ^member/photo/render_fast(.*) /photo_render.php?r=$1 [L]
# otherwise forward it to index.php
RewriteRule . index.php [L]

但这不起作用,当我在本地机器上尝试时,我的 Apache 总是使用第二条规则,而在我的托管服务器中,Apache 总是显示错误。

我做错了什么?

【问题讨论】:

  • 您的第一条规则说:如果 URL 以 member/photo/render_fast 开头并且请求的资源是 NOT 文件 而不 文件夹,则将其转发到/photo_render.php。您的第二条规则说:将所有内容重定向到/index.php

标签: apache .htaccess mod-rewrite


【解决方案1】:

发生的情况是第二次(在 URI 被重写为 /photo_render.php 之后)应用第二条规则,因此 /photo_render.php 被重写为 index.php。当您尝试匹配 ..* 之类的内容时,您需要添加一些条件。这应该有效:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^member/photo/render_fast(.*) /photo_render.php?r=$1 [L]
# otherwise forward it to index.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

请注意,如果由于某种原因您没有 photo_render.php,这也会失败。

【讨论】:

    猜你喜欢
    • 2021-11-29
    • 2012-04-02
    • 1970-01-01
    • 2021-12-08
    • 2021-09-28
    • 2013-02-14
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    相关资源
    最近更新 更多