mod_rewrite problems: negation


I'm trying to understand mod_rewrite better and have one particular problem I think I need to get my head round first.

I am rewriting http://www.somesite.tld/a/b/c to index.php?path=a/b/c using the following

RewriteRule ^(?!index.php)(.*)$ index.php?path=$1 [NC,L]

An equivalent rewrite would, in this case, be

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NC,L]
  • This does not work without the RewriteCond -- path=index.php would be the result without specifically ignoring files or saying 'not index.php'. Why is this?
  • Also, what is the ?! and ?: syntax that I sometimes see used? I do not understand the use of the ? when it is not prefixed by anything.
  • And why, in the first RewriteRule above, do the second pair of brackets return a match for $1?


By : damian86


"!" means negation. Like a = 1 (a is equal one) a != 1 (a is not equal one); "f" means file. So if you use together with "!", like "!-f" would be something "file does not exist". the links below may help you better:

By : tenshimsm

This video can help you solving your question :)
By: admin