Petruza Petruza - 1 year ago 119
Apache Configuration Question

$1 substitution for Apache's RewriteRule doesn't return the first matched expression

I've read everywhere that pattern substitution in Apache's Rewrite Engine works as expected for regular expressions, so I tried:


RewriteRule (.*) index.php?route=$1

But for a request to
in the get var route I get
instead of

In the http var
I get

But in
I get

What could be wrong here?

also returns

If I use
RewriteRule . index.php?route=$1
, I get route=
, whatever the request URL.

Answer Source

$1 is working as expected but problem is your use of this pattern:


which matches anything. Your rewrite rule actually loops and runs twice because you don't have any RewriteCond to precent looping.

  1. First it runs for URI=some/url and URI becomes index.php while $1 becomes some/url
  2. Second time it runs for URI=index.php and $1 becomes index.php

You can use this rule to fix this behavior:

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?route=$1 [L,QSA]

OR if you want to rewrite existing files and directories to index.php then use:

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

This will rewrite everything except index.php to index.php.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download