rewriting a url with a ? in it not working

Maxim Dounin mdounin at mdounin.ru
Sun Dec 20 19:06:09 MSK 2009


Hello!

On Sun, Dec 20, 2009 at 05:41:50AM -0500, jimxms wrote:

> Hi All,
> 
> I've just recently moved over to nginx, so please excuse my stupidity if this is a really easy one to solve.
> 
> I have the following URL:
> 
> www.domain.net/content.php?/section/content_title/1
> 
> which I am trying to rewrite to:
> 
> www.domain.net/content/section/content_title/1
> 
> I'm using the following in my nginx config:
> 
> rewrite ^/content.php?/(\w*)/(\w*)/(\w*)$ /content/$1/$2/$3 last;

rewrite doesn't act on query string (aka args), only on path.  To 
match args you have to use if().

Try something like this:

    location = /content.php {
        if ($args ~ "^/(\w*/\w*/\w*)$") {
            set $x $1;
            rewrite ^ /content/$x? last;
        }
        rewrite ^ /content/? last;
    }

> however when I visit the URL to be rewritten the vars $1 $2 $3 are not being passed to the content script. Interestingly though, if I visit:
> 
> www.domain.net/content.php/section/content_title/1  (notice the lack of ?)
> 
> It works fine.

This is what you wrote in your regexp, so no surprise here.

Maxim Dounin



More information about the nginx mailing list