试图在 .htaccess 中对 RewriteRule 设置一个例外 [英] Trying to put an exception to RewriteRule in .htaccess

查看:27
本文介绍了试图在 .htaccess 中对 RewriteRule 设置一个例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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=$1 [QSA,L]

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

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 RewriteCond before, so I'm a little unsure if what I am doing is correct.

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

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="

我做错了什么?谢谢

推荐答案

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

So you just need to ignore http://yourdomain.com/market-reports (in addition to files/directories?). You should be fine with:

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

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

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

^"符号充当匹配字符串开头和!"的锚点否定匹配,因此任何与表达式其余部分不匹配的字符串 URL 都将被重写为其他指定的规则.请参阅 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

这篇关于试图在 .htaccess 中对 RewriteRule 设置一个例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆