# Rewriting a url with a ? in it not working

**URL:** <https://www.ruby-forum.com/t/rewriting-a-url-with-a-in-it-not-working/180494>\
**Category:** NGINX\
**Created:** [December 20, 2009, 11:45am UTC](https://www.ruby-forum.com/t/rewriting-a-url-with-a-in-it-not-working/180494 "2009-12-20T11:45:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimxms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jimxms](https://www.ruby-forum.com/u/jimxms)\
**Post date:** [December 20, 2009, 11:45am UTC](https://www.ruby-forum.com/t/rewriting-a-url-with-a-in-it-not-working/180494/1 "2009-12-20T11:45:00Z")

</div>

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](http://www.domain.net/content.php?/section/content_title/1)

which I am trying to rewrite to:

[www.domain.net/content/section/content\_title/1](http://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;

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](http://www.domain.net/content.php/section/content_title/1) (notice the lack of  
?)

It works fine.

Any assistance much appreciated.

Posted at Nginx Forum:

> **[rewriting a url with a ? in it not working](https://forum.nginx.org/read.php?2,32076,32076#msg-32076)**
>
> 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:

---

<div class="post-metadata">

**Author:** ![jimxms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jimxms](https://www.ruby-forum.com/u/jimxms)\
**Post date:** [December 20, 2009, 11:47am UTC](https://www.ruby-forum.com/t/rewriting-a-url-with-a-in-it-not-working/180494/2 "2009-12-20T11:47:56Z")

</div>

quick regexp attempt -

try escaping the ? - try ?

On Sun, Dec 20, 2009 at 2:41 AM, jimxms [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

- rewrite ^/content.php?/(\w\*)/(\w\*)/(\w\*)$ /content/$1/$2/$3 last;

- rewrite ^/content.php?/(\w\*)/(\w\*)/(\w\*)$ /content/$1/$2/$3 last;

---

<div class="post-metadata">

**Author:** ![jimxms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jimxms](https://www.ruby-forum.com/u/jimxms)\
**Post date:** [December 20, 2009, 12:07pm UTC](https://www.ruby-forum.com/t/rewriting-a-url-with-a-in-it-not-working/180494/3 "2009-12-20T12:07:15Z")

</div>

Sorry, I should have said that I tried that already ☹

## mike Wrote:

> /content/$1/$2/$3 last;
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,32076,32083#msg-32083](http://forum.nginx.org/read.php?2,32076,32083#msg-32083)

---

<div class="post-metadata">

**Author:** ![jimxms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jimxms](https://www.ruby-forum.com/u/jimxms)\
**Post date:** [December 20, 2009, 5:09pm UTC](https://www.ruby-forum.com/t/rewriting-a-url-with-a-in-it-not-working/180494/4 "2009-12-20T17:09:51Z")

</div>

Hello!

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

> [www.domain.net/content/section/content\_title/1](http://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](http://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 D.

---

<div class="post-metadata">

**Author:** ![jimxms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jimxms](https://www.ruby-forum.com/u/jimxms)\
**Post date:** [December 20, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/rewriting-a-url-with-a-in-it-not-working/180494/5 "2009-12-20T14:55:48Z")

</div>

On Sun, Dec 20, 2009 at 5:41 PM, jimxms [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [www.domain.net/content/section/content\_title/1](http://www.domain.net/content/section/content_title/1)
> 
> Any assistance much appreciated.
> 
> Posted at Nginx Forum: [http://forum.nginx.org/read.php?2,32076,32076#msg-32076](http://forum.nginx.org/read.php?2,32076,32076#msg-32076)

I believe the arguments are not included in rewrite base string.

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)
