apache .htaccess 重写正则表达式:匹配除“索引”之外的任何内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1548054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:14:51  来源:igfitidea点击:

.htaccess rewrite regex: match anything but "index"

regexapache.htaccess

提问by jezmck

I'm trying to create a rule used in a .htaccess file to match anything buta particular string, in this case: index.

我试图创建一个.htaccess文件用来匹配规则什么,但一个特定的字符串,在这种情况下:index

I thought that it should be possible to match this special string first and use [L]but that's not working,
and it should be possible using the following regex, but it causes a 500 error.

我认为应该可以首先匹配这个特殊字符串并使用,[L]但这不起作用,
并且应该可以使用以下正则表达式,但它会导致 500 错误。

I want to match:

我想匹配:

  • pagename1
  • pagename2/whatever
  • pagename3/234/whatever
  • about
  • contact-us
  • (etc)
  • 页名1
  • 页名2/随便
  • pagename3/234/随便
  • 关于
  • 联系我们
  • (等等)

but not

但不是

  • index/123
  • index/124/whatever
  • 索引/123
  • 指数/124/随便

(BTW "index" is the name of a file with no extension, not my choice, this is a work thing)

(顺便说一句“索引”是没有扩展名的文件名,不是我的选择,这是一个工作的东西)

^(?!index)[\w/\-]+

I assume that apache's implementation of regex doesn't cope with the (?!xxx)rule.

我认为 apache 的 regex 实现不符合(?!xxx)规则。

Any help/suggestions will be much appreciated.

任何帮助/建议将不胜感激。

回答by Vinko Vrsalovic

You can use RewriteConds to do this, for example:

您可以使用RewriteConds 来执行此操作,例如:

RewriteCond %{REQUEST_URI} !^index
RewriteRule (.*) index.php?what=

That will rewrite every URL which doesn't start by index. If you want it to avoid it anywhere in the URL, remove the ^

这将重写每个不是以索引开头的 URL。如果您希望它避免在 URL 中的任何位置使用它,请删除^

回答by Arjan

(Please read the comments. I doubt it and I've never encountered problems, while it gives one much cleaner .htaccessfiles, but: using RewriteCondmightbe preferred over using RewriteRulewith a dash, due to how rulesets are processed?)

(请阅读评论。我对此表示怀疑,我从未遇到过问题,虽然它提供了一个更清晰的.htaccess文件,但是:由于规则集的处理方式,使用RewriteCond可能比使用RewriteRule破折号更受欢迎?)

Just use RewriteRule as follows, to notrewrite (the dash), and stop subsequent rewriting (the [L]) for anything that starts with index/:

只需按如下方式使用 RewriteRule,不要重写(破折号),并停止[L]对以 开头的任何内容进行后续重写()index/

# Do not rewrite anything that starts with index/
RewriteRule ^index/ - [L]

After this, do any rewriting you like.

在此之后,进行任何您喜欢的重写。

Or maybe even limit some more first:

或者甚至可以先限制更多:

# Do not rewrite files, directories or symbolic links 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d [OR] 
RewriteCond %{REQUEST_FILENAME} -l 
RewriteRule . - [L]

回答by Gumbo

Apache used three different regular expression implementations. The first was System V8, since Apache 1.3 they used POSIX ERE and since Apache 2 they use PCRE. And only PCRE supports look-ahead assertions. So you need Apache 2 to use that rule.

Apache 使用了三种不同的正则表达式实现。第一个是 System V8,从 Apache 1.3 开始,他们使用 POSIX ERE,从 Apache 2 开始,他们使用 PCRE。并且只有 PCRE 支持前瞻断言。因此,您需要 Apache 2 才能使用该规则。

But now to your question. If you use this rule:

但现在你的问题。如果您使用此规则:

RewriteRule ^index/ - [L]

anything that starts with /index/should be catched by this rule and no further rule should be applied.

任何开头/index/应该通过这条规则被获取,并没有进一步的规则应适用。

But if that doesn't work, try this:

但如果这不起作用,请尝试以下操作:

RewriteRule !^index/ …

Again, this rule will be applied on any request that's URL path doesn't start with /index/.

同样,此规则将应用于 URL 路径不以 开头的任何请求/index/

And if you want to capture anything from the URL, use a RewriteCondcondition to test either the full URL path itself (%{REQUEST_URI}) or just the match of one of your pattern groups:

如果您想从 URL 中捕获任何内容,请使用RewriteCond条件来测试完整的 URL 路径本身 ( %{REQUEST_URI}) 或您的模式组之一的匹配项:

RewriteCond %{REQUEST_URI} !^/index/
RewriteRule (.*) …
# or
RewriteCond  !^index$
RewriteRule ^([^/]+)/(.*) …