重写规则贪婪 [英] RewriteRule being greedy

查看:146
本文介绍了重写规则贪婪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找了几个小时回答了,很抱歉,如果这个被问得吨的时候,我错过了。

I have been looking for an answer for a few hours now, so sorry if this was asked a ton of times, I missed it.

基本上,我想打一个重写忽略的第一个目录。在路径第一个目录将是不同的,所以我想我可以使用正则表达式。但我的正则表达式是一路匹配文件名:

I basically want to make a rewrite to ignore the first directory. That first dir in the path will be different so I thought I could use a regex. But my regex is matching all the way to the file name:

RewriteRule ^([a-z]+)?/(.+)$ $2 [L]

这工作,如果我是一个级别深:

this works if I am one level deep:

http://test.domain.com/one/index.php

我得到根本的实际索引页。这就是我想要的。但如果我是去更深层次的:

I get the actual index page of the root. Which is what I want. but if I were to go deeper:

http://test.domain.com/one/two/anotherfile.php

我得到一个消息,说/anotherfile.php没有被发现,因为它正在寻找根吧。因此,似乎我正则表达式在最后[A-Z]未停止。我AP preciate任何帮助。

I get a message saying /anotherfile.php was not found, because it is looking in the root for it. So it seems my regex is not stopping after the last [a-z]. I appreciate any help.

这是Apache2的,如果是重要的。

This is Apache2 if that matters at all.

推荐答案

重写引擎重复所有规则,直到URI是相同的前,通过规则的迭代后。鉴于规则,重写规则^([AZ] +)/(+)$ $ 2 [L] ,输入:<?code> HTTP:/ /test.domain.com/one/index.php ,这是发生了什么:

The rewrite engine repeats all the rules until the URI is the same before and after an iteration through the rules. Given the rule, RewriteRule ^([a-z]+)?/(.+)$ $2 [L], and the input: http://test.domain.com/one/index.php, this is what's happening:


  1. 的斜线(preFIX)从URI删除:单/ index.php文件

  2. 的规则应用,URI匹配 ^([A-Z] +)?/(+)$

  3. URI被改写为的index.php

  4. 内部重定向,新的URI(的index.php )不匹配,老URI( /one/index.php ),并通过重写规则跑回

  5. 领先的斜线被删除

  6. 的规则应用,URI做的的匹配 ^([A-Z] +)?/(+)$

  7. URI不重写。新的URI(的index.php )是一样的老URI通过重写引擎(的index.php ),改写停止

  1. The leading slash (prefix) is removed from the URI: one/index.php
  2. The rule is applied, URI matches ^([a-z]+)?/(.+)$
  3. URI is rewritten to /index.php
  4. Internal redirect, new URI (/index.php) doesn't match old URI (/one/index.php) and is run back through the rewrite rules
  5. leading slash is removed
  6. The rule is applied, URI does not match ^([a-z]+)?/(.+)$
  7. URI is not rewritten. new URI (/index.php) is same as old URI before being run through the rewrite engine (/index.php), rewriting stops

结果URI是的index.php

但随着输入: http://test.domain.com/one/two/anotherfile.php


  1. 的斜线(preFIX)从URI删除:一/二/ anotherfile.php

  2. 的规则应用,URI匹配 ^([A-Z] +)?/(+)$

  3. URI被改写为 /two/anotherfile.php

  4. 内部重定向,新的URI( /two/anotherfile.php )不匹配,老URI( /one/two/anotherfile.php ),并通过重写规则跑回

  5. 斜线被删除(二/ anotherfile.php

  6. 的规则appled,URI匹配 ^([A-Z] +)?/(+)$

  7. URI被改写为 /anotherfile.php

  8. 内部重定向,新的URI( /anotherfile.php )不匹配,老URI( /two/anotherfile.php ),并通过重写规则跑回

  9. 斜线被删除( anotherfile.php

  10. 的规则应用,URI做的的匹配 ^([A-Z] +)?/(+)$

  11. URI不重写。新的URI( /anotherfile.php )是一样的老URI通过重写引擎( /anotherfile.php ),改写停止

  1. The leading slash (prefix) is removed from the URI: one/two/anotherfile.php
  2. The rule is applied, URI matches ^([a-z]+)?/(.+)$
  3. URI is rewritten to /two/anotherfile.php
  4. Internal redirect, new URI (/two/anotherfile.php) doesn't match old URI (/one/two/anotherfile.php) and is run back through the rewrite rules
  5. leading slash is removed (two/anotherfile.php)
  6. The rule is appled, URI matches ^([a-z]+)?/(.+)$
  7. URI is rewritten to /anotherfile.php
  8. Internal redirect, new URI (/anotherfile.php) doesn't match old URI (/two/anotherfile.php) and is run back through the rewrite rules
  9. leading slash is removed (anotherfile.php)
  10. The rule is applied, URI does not match ^([a-z]+)?/(.+)$
  11. URI is not rewritten. new URI (/anotherfile.php) is same as old URI before being run through the rewrite engine (/anotherfile.php), rewriting stops

结果URI是 /anotherfile.php

[L] 不被放回通过重写引擎停止得到的URI,它只是停止当前迭代重写的过程。您需要更改正规前pression或添加某种重写条件。一种可能性是将重写规则^ [A-Z +] /([AZ] +)?/(+)$ $ 2 [L] 你有这样的一前2深目录得到单独处理。

The [L] doesn't stop the resulting URI from being put back through the rewrite engine, it just stops the rewriting process for the current iteration. You need to change the regular expression or add some kind of rewrite condition. One possibility is adding RewriteRule ^[a-z+]/([a-z]+)?/(.+)$ $2 [L] before the one that you have so that 2 deep directories get handled separately.

这篇关于重写规则贪婪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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