【问题标题】:Apache redirect regexp: match something that is not following somethingApache重定向正则表达式:匹配不遵循某些东西的东西
【发布时间】:2011-10-28 09:10:53
【问题描述】:

什么正则表达式应该代表“所有不遵循'index.php'的东西都应该指向'index.php/$1'”?

例如,“http://mysite/moo”应该指向“http://index.php/moo”,而“http://index.php/moo”不应该。

我目前正在使用 PCRE 正则表达式(Apache 文档说应该在此处使用 PCRE)无效:RedirectMatch (?<=index\.php)(\/.+) /index.php/$1

什么是正确的?

UPD:重点是只使用 mod_alias,省略 mod_rewrite

【问题讨论】:

  • 您拥有的?<= 的否定是?<!
  • @mario, RedirectMatch (?<=index\.php)(\/.+) /index.php/$2 将永久重定向到/index.php/。你能给点建议吗?

标签: regex apache pcre


【解决方案1】:

您可以使用带有不匹配模式(“!”前缀)的RedirectCond。试试:

RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule (.*) index.php/$1

或者:

RewriteCond $1 !^/?index.php
RewriteRule (.*) index.php/$1

【讨论】:

  • 对不起,我不需要 mod_rewrite =)
【解决方案2】:

也许:

^/?(?!index\.php)(.+)

即负前瞻

【讨论】:

  • 似乎与 URL 匹配,但重定向规则应该是什么? /index.php/$1 递归地给出无穷无尽的 URL =)
【解决方案3】:

我只尝试了一个简单的重定向到静态站点(有效):

RedirectMatch   ^(?!/index\.htm$).*$    /index.htm

基于此,以下可能会对您有所帮助(我自己没有尝试过):

RedirectMatch   ^(?!/index\.php$)(.*)$    /index.php$1

See also: Mastering Lookahead and Lookbehind

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2011-02-27
    • 2016-10-19
    • 2021-01-19
    相关资源
    最近更新 更多