.htaccess rewrite regex: match anything but “index”

后端 未结 3 1880
孤城傲影
孤城傲影 2021-02-20 00:31

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

I thought that it sho

3条回答
  •  面向向阳花
    2021-02-20 00:45

    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.

    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.

    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/.

    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:

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

提交回复
热议问题