.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"
- "foobar" matches
([^?]*)
, uri gets rewritten "/script.php?path=foobar" - rewrite engine loops
- "script.php" matches
([^?]*)
, uri gets rewritten "/script.php?path=script" - rewrite engine loops
- etc.
now if add condition:
rewritecond %{request_filename} !-f rewriterule ([^?]*) /script.php?path=$1 [l,qsa]
and request comes in /foobar
uri "foobar"
- "foobar" not file exists,
!-f
true - "foobar" matches
([^?]*)
, uri gets rewritten "/script.php?path=foobar" - rewrite engine loops
- "script.php" is file exists,
!-f
false - condition false rule isn't applied. rewriting stops, resulting uri "/script.php?path=foobar"
Comments
Post a Comment