【问题标题】:mod_rewrite rule to match exact URL onlymod_rewrite 规则仅匹配确切的 URL
【发布时间】:2011-06-10 14:34:42
【问题描述】:

我遇到了mod_rewrite 的问题,我想在其中匹配并替换特定的 URL。我要重写的网址是:

http://example.com/rsshttp://example.com/rss.php

这意味着,如果有人在rss 之后附加任何内容,则会发送 404 Not Found 响应。目前我正在使用这个mod_rewritesn-p:

Options -Indexes

RewriteEngine on
RewriteBase /

# pick up request for RSS feed
RewriteRule ^rss/?$ rss.php [L,NC]

# pass any other request through CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) index.php/$1

但这匹配 rssrss 以及添加到末尾的任何其他内容。我怎样才能重写以上内容以访问 only http://example.com/rss 作为 mod_rewrite 匹配的模式?

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    您收到此错误是因为 /rss 在您的规则中被两个 RewriteRules 重定向了两次。有这样的规则:

    Options +FollowSymlinks -MultiViews -Indexes
    RewriteEngine On
    RewriteBase /
    
    # pick up request for RSS feed
    RewriteRule ^rss/?$ /rss.php [L,NC]
    
    # pass any other request through CMS
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (?!^rss\.php$)^(.*)$ /index.php/$1 [L,NC]
    

    因此,根据上述规则,它会将/rssrss/ URI 重定向到/rss.php,但是/rss/foo 将被重定向到/index.php,因为您的第二条规则是将一切 转发到@987654328 @

    【讨论】:

      【解决方案2】:

      看到您的规则不起作用,我很惊讶,因为在我第一次尝试时,我会得出一个非常相似的解决方案。但是查看重写日志发现了真正的问题。

      正如here 所述,服务器更喜欢真实文件而不是目录。所以在应用重写规则时,内部rss/something 变成了rss.php/something,事情变得很奇怪。

      因此,一种解决方案是检查是否在 .htaccess 或 vhost 配置中为 Web 目录启用了选项 MultiViews。如果是这样,请将其删除 - 这在此示例中对我有用。

      如果您需要MultiViews,那么我想唯一的机会是将rss.php 重命名为rss-content.php 并相应地更改规则。

      另外一个注意事项:您可能希望在 # ... CMS 块之后添加以下行,以防止无休止的递归调用。

      RewriteRule ^index\.php/.* - [PT,L]
      

      我希望这能解决你的重写问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-29
        • 1970-01-01
        • 2010-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-03
        • 1970-01-01
        相关资源
        最近更新 更多