.htaccess rewrite regex: match anything but "index"

.htaccess, apache, regex

Solution

You can use `RewriteCond`s to do this, for example:

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 `^`

Problem

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 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. I want to match: - pagename1 - pagename2/whatever - pagename3/234/whatever - about - contact-us - (etc) but not - index/123 - index/124/whatever (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. Any help/suggestions will be much appreciated.

Original source