的RewriteCond在.htaccess与否定的正则表达式条件不工作? [英] RewriteCond in .htaccess with negated regex condition doesn't work?

查看:314
本文介绍了的RewriteCond在.htaccess与否定的正则表达式条件不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想prevent,在这种情况下,Word中preSS,从此改写了某些网址。在这种情况下,我试图prevent从以往处理的上传目录的要求,而是留给那些服务器的404页。所以我假设它的简单,只要添加的规则:

I'm trying to prevent, in this case WordPress, from rewriting certain URLs. In this case I'm trying to prevent it from ever handling a request in the uploads directory, and instead leave those to the server's 404 page. So I'm assuming it's as simple as adding the rule:

RewriteCond %{REQUEST_URI} !^/wp-content/uploads/

这个规则应该评估,以虚假,使链的规则对这些请求失败,这样就停止了重写。但是,没有。也许我需要匹配覆盖整个字符串我的前pression?

This rule should evaluate to false and make the chain of rules fail for those requests, thus stopping the rewrite. But no... Perhaps I need to match the cover the full string in my expression?

RewriteCond %{REQUEST_URI} !^/wp-content/uploads/.*$

不,这不是它的。所以,抓我的头后,我做理智的检查。也许,什么是错的实际模式。所以,我做一个简单的测试用例。

Nope, that's not it either. So after scratching my head I do a check of sanity. Perhaps something is wrong with the actual pattern. So I make a simple test case.

RewriteCond %{REQUEST_URI} ^/xyz/$

在这种情况下,重写发生当且仅当所请求的URL是/ XYZ /和显示服务器404页为任何其他页面。这正是我所期待的。所以我就坚持了!否定这一模式。

In this case, the rewrite happens if and only if the requested URL is /xyz/ and shows the server's 404 page for any other page. This is exactly what I expected. So I'll just stick in a ! to negate that pattern.

RewriteCond %{REQUEST_URI} !^/xyz/$

现在,我期待看到上述情况完全相反。重写不应该发生的/ XYZ /但对于所有其他可能的URL。相反,发生重写每一个URL,无论/ XYZ /等。

Now I'm expecting to see the exact opposite of the above condition. The rewrite should not happen for /xyz/ but for every other possible URL. Instead, the rewrite happens for every URL, both /xyz/ and others.

所以,无论是在RewriteConds使用否定正则表达式是在Apache的打破,或者有一些基本的我不知道这件事。哪一个是什么呢?

So, either the use of negated regexes in RewriteConds is broken in Apache, or there's something fundamental I don't understand about it. Which one is it?

该服务器是Apache2的。

The server is Apache2.

在全部文件:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/wp-content/uploads/
RewriteRule . /index.php [L]
</IfModule>

字preSS的默认文件再加上我的规则。

WordPress's default file plus my rule.

推荐答案

于是,经过大量的刺激,我想通了这个问题,有点。事实证明,在我原来的问题的规则其实也正是它应该。所以做了一些做同样的事情的其他方式,如

So, after a lot of irritation, I figured out the problem, sort of. As it turned out, the rule in my original question actually did exactly what it was supposed to. So did a number of other ways of doing the same thing, such as

RewriteRule ^wp-content/uploads/.*$ - [L]

(标记规则在去年,如果模式匹配)或者

(Mark rule as last if pattern matches) or

RewriteRule ^wp-content/uploads/.*$ - [S=1]

(跳到下一个规则,如果模式匹配),以及在这个问题的否定规则,如提及。所有这些规则工作得很好,并且无需重写控制返回到Apache。

(Skip the next rule if pattern matches) as well as the negated rule in the question, as mentioned. All of those rules worked just fine, and returned control to Apache without rewriting.

在这些规则进行了处理的问题发生了。相反,问题是,我删除了我的主机提供一个默认的404.shtml,403.shtml等模板。如果你没有任何的.htaccess重写,这工作得很好;服务器将盛起自己的默认404页面,一切正常。 (至少这是我的想法,但事实上它是双重错误此外,在试图使用ErrorDocument来处理请求404未找​​到时遇到错误。)

The problem happened after those rules were processed. Instead, the problem was that I deleted a the default 404.shtml, 403.shtml etc templates that my host provided. If you don't have any .htaccess rewrites, that works just fine; the server will dish up its own default 404 page and everything works. (At least that's what I thought, but in actual fact it was the double error "Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.")

当你有一个htaccess的,而另一方面,则执行第二次为404页。如果该页面是存在的,它会被使用,但现在,而不是为404.shtml请求被抓获的包罗万象的规则,并重新写入的index.php。出于这个原因,所有其他的建议,我在这里得到,或其他地方,都因为最终的404页面已被重写的index.php失败。

When you do have a .htaccess, on the other hand, it is executed a second time for the 404 page. If the page is there, it will be used, but now, instead the request for 404.shtml was caught by the catch-all rule and rewritten to index.php. For this reason, all other suggestions I've gotten here, or elsewhere, have all failed because in the end the 404 page has been rewritten to index.php.

因此​​,该解决方案只是用来恢复错误模板。现在回想起来那是pretty的愚蠢删除它们,但我有这样的从零开始的心态。什么都不想看似不必要的躺在身边。至少现在我明白发生了什么事情,这就是我想要的。

So, the solution was simply to restore the error templates. In retrospect it was pretty stupid to delete them, but I have this "start from scratch" mentality. Don't want anything seemingly unnecessary lying around. At least now I understand what was going on, which is what I wanted.

最后评论到丝丝:我从没想过要禁止访问任何东西,从发生只停留改写。不,它现在很多重要的,但我只是想澄清这一点。

Finally a comment to Cecil: I never wanted to forbid access to anything, just stop the rewrite from taking place. Not that it matters much now, but I just wanted to clarify this.

这篇关于的RewriteCond在.htaccess与否定的正则表达式条件不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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