apache 试图在 .htaccess 中对 RewriteRule 设置一个例外
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2226364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to put an exception to RewriteRule in .htaccess
提问by alex
I am redirecting all requests like so:
我正在像这样重定向所有请求:
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= [QSA,L]
All my incoming links are meant to go to index.php, as you can see. But now I want to stop one from going there. I've never written my own RewriteCondbefore, so I'm a little unsure if what I am doing is correct.
如您所见,我所有的传入链接都指向 index.php。但现在我想阻止一个人去那里。我以前从未写过自己的东西RewriteCond,所以我有点不确定我所做的是否正确。
Basically what I'm trying to say is: "If incoming URL is a file, directory or /market-reports/ do nothing. Otherwise send on the URL to index.php?section="
基本上我想说的是:“如果传入的 URL 是一个文件、目录或 /market-reports/ 什么都不做。否则将 URL 发送到 index.php?section="
What am I doing wrong? Thanks
我究竟做错了什么?谢谢
回答by Owen
So you just need to ignore http://yourdomain.com/market-reports(in addition to files/directories?). You should be fine with:
所以你只需要忽略http://yourdomain.com/market-reports(除了文件/目录?)。你应该没问题:
RewriteCond %{REQUEST_URI} !^/market-reports/?$
This will (not) match "http://yourdomain.com/market-reports" as well as "http://yourdomain.com/market-reports/" as the question mark "?", in the Perl Compatible Regular Expression vocabulary that mod_rewrite uses, makes the match optional (a wildcard) before the end of the string anchor, which is represented with the literal dollar sign "$".
这将(不)匹配“ http://yourdomain.com/market-reports”以及“ http://yourdomain.com/market-reports/”作为问号“?”,在 Perl 兼容正则表达式中mod_rewrite 使用的词汇,使匹配可选(通配符)在字符串锚点的末尾之前,用文字美元符号“$”表示。
The "^" symbol acts as an anchor matching the beginning of the string and the "!" negates the match, so that any string URL that does not match the rest of the expression will be rewritten to the other specified rules. See mod_rewrite regex vocabulary
“^”符号充当匹配字符串开头和“!”的锚点。否定匹配,因此任何与表达式其余部分不匹配的字符串 URL 将被重写为其他指定的规则。请参阅mod_rewrite 正则表达式词汇表
回答by SkuterPL
Why it doesn't work with above this code:
为什么它不适用于上面的代码:
RedirectMatch ^/([^\.]+)/?$ http://domain.pl/.html
I try to add .html to all links without it, but except CMS
我尝试将 .html 添加到所有没有它的链接,但除了 CMS

