Httpd – Apache mod_rewrite remove question mark (?) and query-string substitution

apache-2.2httpdmod-rewrite

I registered a RewriteRule with mod_rewrite, but the substituted URL are wrong.

The rule are:

RewriteRule ^/img/z(0|1)u(.+)\.(gif|jpg|jpeg|png)$ /image-servlet/img/?z=$1&url=$2 [NE,QSA,PT,T=image/$3]

When I access the following URL:

http://localhost/img/z1uABC.jpg

The applied rule redirect to:

http://localhost/image-servlet/img/ABC

The final URL miss the query-string substitution:

?z=$1&url=$2

And are replaced just by:

$2

Anybody can help me with my mistake?

Thank,

And Past

Best Answer

I know it's ugly, and even more inefficient, but have you tried:

RewriteRule ^/img/z(.+)u(.+)\.(.+)$ /image-servlet/img/?z=$1&url=$2 [NE,QSA,PT,T=image/$3]

I seem to remember that mod_rewrite will only match (.+) or (.*) to a variable. Obviously (from your example), it's the only thing that's being correctly rewritten.

Of course, that mod_rewrite rule will take forever to match, and requires that the script do some input validation.

Also, apache supports the TRACE http command, which is good for tracking down multiple (accidental) rewrites. Just swap the GET in the http request with TRACE, for example:

TRACE /z1uABC.jpg HTTP/1.1

Host: foohost.com

Lastly, if you're still stuck, try running the LiveHeader or Firebug firefox extensions.