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

时间:2021-10-21 11:04:08

I'm trying to create a rule used in a .htaccess file to match anything but a 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
  • pagename1
  • pagename2/whatever
  • pagename2 /什么
  • pagename3/234/whatever
  • pagename3 / 234 /什么
  • about
  • 关于
  • contact-us
  • 联系我们
  • (etc)
  • (等等)

but not

但不是

  • index/123
  • 索引/ 123
  • index/124/whatever
  • 索引/ 124 /不管

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

(BTW“索引”是没有扩展名的文件的名称,不是我的选择,这是一个工作的事情)

^(?!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.

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

3 个解决方案

#1


15  

You can use RewriteConds to do this, for example:

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

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

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中的任何位置避开它,请删除^

#2


4  

(Please read the comments. I doubt it and I've never encountered problems, while it gives one much cleaner .htaccess files, but: using RewriteCond might be preferred over using RewriteRule with a dash, due to how rulesets are processed?)

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

Just use RewriteRule as follows, to not rewrite (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]

#3


1  

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 RewriteCond condition 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 $1 !^index$
RewriteRule ^([^/]+)/(.*) …

#1


15  

You can use RewriteConds to do this, for example:

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

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

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中的任何位置避开它,请删除^

#2


4  

(Please read the comments. I doubt it and I've never encountered problems, while it gives one much cleaner .htaccess files, but: using RewriteCond might be preferred over using RewriteRule with a dash, due to how rulesets are processed?)

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

Just use RewriteRule as follows, to not rewrite (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]

#3


1  

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 RewriteCond condition 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 $1 !^index$
RewriteRule ^([^/]+)/(.*) …