# Rewrite automatically unencodes question mark

**URL:** <https://www.ruby-forum.com/t/rewrite-automatically-unencodes-question-mark/221990>\
**Category:** NGINX\
**Created:** [August 8, 2012, 1:08pm UTC](https://www.ruby-forum.com/t/rewrite-automatically-unencodes-question-mark/221990 "2012-08-08T13:08:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [August 8, 2012, 1:08pm UTC](https://www.ruby-forum.com/t/rewrite-automatically-unencodes-question-mark/221990/1 "2012-08-08T13:08:48Z")

</div>

Hello,

I have this rewrite rule.

rewrite ^(.+)$ $url? permanent;

where $url is  
“[http://example.com/ez/ciaapkznke/&subid1=1&lnkurl=http://www.someotherwebsite.com/books%3Faffid%3Dtyroo%26cmpid%3Daffiliate\_promo\_tyroo](http://example.com/ez/ciaapkznke/&subid1=1&lnkurl=http://www.someotherwebsite.com/books%3Faffid%3Dtyroo%26cmpid%3Daffiliate_promo_tyroo)”

However its redirected to

where $url is  
“[http://example.com/ez/ciaapkznke/&subid1=1&lnkurl=http://www.someotherwebsite.com/books?affid%3Dtyroo%26cmpid%3Daffiliate\_promo\_tyroo](http://example.com/ez/ciaapkznke/&subid1=1&lnkurl=http://www.someotherwebsite.com/books?affid%3Dtyroo%26cmpid%3Daffiliate_promo_tyroo)”

%3F got changed to ? which i dont intend to…

Any help?

Posted at Nginx Forum:

> **[Rewrite automatically unencodes question mark.](https://forum.nginx.org/read.php?2,229436,229436#msg-229436)**
>
> Hello, I have this rewrite rule. rewrite ^(.+)$ $url? permanent; where $url is "http://example.com/ez/ciaapkznke/&subid1=1&lnkurl=http://www.someotherwebsite.com/books%3Faffid%3Dtyroo%26cmpid%3Daffiliate\_promo\_tyroo" Howev

---

<div class="post-metadata">

**Author:** ![jimishjoban](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jimishjoban](https://www.ruby-forum.com/u/jimishjoban)\
**Post date:** [August 8, 2012, 7:13pm UTC](https://www.ruby-forum.com/t/rewrite-automatically-unencodes-question-mark/221990/2 "2012-08-08T19:13:31Z")

</div>

It seems only the first %3F gets un-encoded. Remaining %3F remains as it  
is… Could it be a bug?

Posted at Nginx Forum:

> **[Re: Rewrite automatically unencodes question mark.](https://forum.nginx.org/read.php?2,229436,229469#msg-229469)**
>
> Hello, I have this rewrite rule. rewrite ^(.+)$ $url? permanent; where $url is "http://example.com/ez/ciaapkznke/&subid1=1&lnkurl=http://www.someotherwebsite.com/books%3Faffid%3Dtyroo%26cmpid%3Daffiliate\_promo\_tyroo" Howev

---

<div class="post-metadata">

**Author:** ![jimishjoban](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jimishjoban](https://www.ruby-forum.com/u/jimishjoban)\
**Post date:** [August 8, 2012, 7:59pm UTC](https://www.ruby-forum.com/t/rewrite-automatically-unencodes-question-mark/221990/3 "2012-08-08T19:59:05Z")

</div>

Hello!

On Wed, Aug 08, 2012 at 01:12:46PM -0400, jimishjoban wrote:

> It seems only the first %3F gets un-encoded. Remaining %3F remains as it  
> is… Could it be a bug?

More or less yes, it’s weird historic behaviour which should be  
eventually fixed (and we even have several TODO test cases  
for this, see  
[nginx-tests: 7a191a1f819d rewrite\_unescape.t](http://mdounin.ru/hg/nginx-tests/file/tip/rewrite_unescape.t)).

For now I would recommend using

```
return 301 $uri;

```

which doesn’t try to do any unescaping.

Maxim D.

---

<div class="post-metadata">

**Author:** ![jimishjoban](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jimishjoban](https://www.ruby-forum.com/u/jimishjoban)\
**Post date:** [August 14, 2012, 4:30pm UTC](https://www.ruby-forum.com/t/rewrite-automatically-unencodes-question-mark/221990/4 "2012-08-14T16:30:03Z")

</div>

Thanks!

that works but would it be possible to execute something else in  
background  
after doing a return?

So I need to execute something which does the redirect and then inserts  
some  
logs in the database… But only after redirect, so its a bit faster…

Posted at Nginx Forum:

> **[Re: Rewrite automatically unencodes question mark.](https://forum.nginx.org/read.php?2,229436,229684#msg-229684)**
>
> Hello, I have this rewrite rule. rewrite ^(.+)$ $url? permanent; where $url is "http://example.com/ez/ciaapkznke/&subid1=1&lnkurl=http://www.someotherwebsite.com/books%3Faffid%3Dtyroo%26cmpid%3Daffiliate\_promo\_tyroo" Howev
