【问题标题】:Trying to put an exception to RewriteRule in .htaccess试图在 .htaccess 中对 RewriteRule 设置一个例外
【发布时间】:2011-01-14 15:27:44
【问题描述】:

我正在像这样重定向所有请求:

RewriteRule ^sitemap.xml$ sitemap.php?/ [QSA,L]

# the line below is the one I'm having trouble with

RewriteCond %{REQUEST_URI}  !^market-reports$ 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?section=$1 [QSA,L]

如您所见,我所有的传入链接都指向 index.php。但现在我想阻止一个人去那里。我以前从未写过自己的RewriteCond,所以我有点不确定我所做的是否正确。

基本上我想说的是:“如果传入的 URL 是文件、目录或 /market-reports/ 什么也不做。否则将 URL 发送到 index.php?section="

我做错了什么?谢谢

【问题讨论】:

  • RewriteRule ^something - [L]

标签: apache .htaccess mod-rewrite


【解决方案1】:

所以你只需要忽略http://yourdomain.com/market-reports(除了文件/目录?)。你应该没问题:

RewriteCond %{REQUEST_URI} !^/market-reports/?$

这将(不)匹配“http://yourdomain.com/market-reports”以及“http://yourdomain.com/market-reports/”作为问号“?”,在 mod_rewrite 使用的 Perl 兼容正则表达式词汇表中,使匹配在之前成为可选(通配符)字符串锚的结尾,用文字美元符号“$”表示。

“^”符号充当匹配字符串开头和“!”的锚点否定匹配,因此任何与表达式的其余部分不匹配的字符串 URL 都将被重写为其他指定的规则。 见mod_rewrite regex vocabulary

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-17
    • 2019-11-28
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多