我怎样才能得到mod_rewrite的只是一次匹配规则 [英] How can I get mod_rewrite to match a rule just once

查看:128
本文介绍了我怎样才能得到mod_rewrite的只是一次匹配规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下网址...

<一个href=\"http://localhost/http.mygarble.com/foundationsofwebprogramming/86\">http://localhost/http.mygarble.com/foundationsofwebprogramming/86

...我想转换成如下:

...that I want to convert into the following:

<一个href=\"http://localhost/http.mygarble.com/php/blog.php?subdomain=foundationsofwebprogramming&page=posts&label=86\">http://localhost/http.mygarble.com/php/blog.php?subdomain=foundationsofwebprogramming&page=posts&label=86

我想我可以用下面的规则做到这一点:

I thought I could achieve this with the following rule:

RewriteRule ([^/]+)/([^/]+)$ php/blog.php?subdomain=$1&page=post&label=$2 [NC,L]

不过我发现的是,这条规则反复作用,导致内部服务器错误。我明白,当URI是使用此规则转化,产生的URI也将匹配的规则,因此被再次应用广告无穷。

However what I find is that this rule is applied repeatedly, resulting in an internal server error. I understand that when the URI is transformed using this rule, the resulting URI will also match the rule, and therefore it is applied again ad-infinitum.

我的previous(诚然相当朦胧)的理解是,[L]标志将停止进一步的处理,虽然我现在明白了,这只是意味着,只有规则的其余部分被跳过,并且不会停止重写发动机通过规则运行一次。

My previous (admittedly rather hazy) understanding was that the [L] flag would stop further processing, although I now understand that this simply means that only the remainder of the rules are skipped, and does not stop the rewrite engine running through the rules again.

我可以通过添加以下条件解决这个问题...

I can fix this problem by adding the following condition...

RewriteCond $0 !php/blog.php
RewriteRule ([^/]+)/([^/]+)$ php/blog.php?subdomain=$1&page=post&label=$2 [NC,L]

...也可以通过写更具体的定期EX pression。但我真正想要做的是找到试图进一步匹配一旦这个规则匹配,一旦停止重写引擎的方式。这可能吗?

...or by writing a more specific regular expression. But what I really want to do is find a way of stopping the rewrite engine from attempting ANY further matches once this rule is matched once. Is this possible?

非常感谢。

推荐答案

用于通常2种方法。

第一个是一个重写条件测试请求的文件不是一个真正的文件。当内部出现递归你的php / blog.php的是一个真正的文件,重写规则不执行第二次。副作用是,对于一个文件,该文件存在将不会被重写任何请求(其可以是很好的副作用)

The first one is a Rewrite Condition testing that the requested file is not a real file. When internal recursion arise your php/blog.php is a real file and rewriterule is not executed the 2nd time. Side-effect is that any request for a file which exists won't be rewritten (which can be good side effect)

RewriteCond %{REQUEST_FILENAME} !-f

第二个解决方案是检查你在一个内部重定向不是:

The second solution is to check you're not in an internal redirection with:

RewriteCond %{ENV:REDIRECT_STATUS} ^$

这第二个解决方案的副作用是,该重写规则不能在其它某些规则之前应用(如果你想一些内部重定向事实上重写第一遍后运行)应用。

Side effect of this 2nd solution is that the rewriteRule cannot be applied if some other rules are applied before (if you want some internal redirection to run after a first pass of rewriting in fact).

<击>
编辑

Edit

有关完成,我将增加第三法:[NS]或[nosubreq]标签似乎是在做同样的事情。 $ P $内部转接后pventing规则使用。

For completion I will add a third method: the [NS] or [nosubreq] tag seems to be doing the same thing. Preventing the rule usage after an internal redirection.

这篇关于我怎样才能得到mod_rewrite的只是一次匹配规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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