.htaccess - Why is RewriteCond needed for this rule to work? -


if have re-write rule:

rewriterule ([^?]*) /script.php?path=$1 [l,qsa] 

it return 500 internal error unless have condition:

rewritecond %{request_filename} !-f 

why this? under impression conditions didn't change how rule works rather exceptions rule.

the -f tests if given argument file , if exists (it 0 bytes in size). reason why 500 internal server error because rewrite engine loops through rules until uri stops changing. example, if had this:

rewriterule ([^?]*) /script.php?path=$1 [l,qsa] 

and request comes in /foobar uri "foobar"

  1. "foobar" matches ([^?]*), uri gets rewritten "/script.php?path=foobar"
  2. rewrite engine loops
  3. "script.php" matches ([^?]*), uri gets rewritten "/script.php?path=script"
  4. rewrite engine loops
  5. etc.

now if add condition:

rewritecond %{request_filename} !-f rewriterule ([^?]*) /script.php?path=$1 [l,qsa] 

and request comes in /foobar uri "foobar"

  1. "foobar" not file exists, !-f true
  2. "foobar" matches ([^?]*), uri gets rewritten "/script.php?path=foobar"
  3. rewrite engine loops
  4. "script.php" is file exists, !-f false
  5. condition false rule isn't applied. rewriting stops, resulting uri "/script.php?path=foobar"

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -