# URL decode rewrite handling: + vs. %20

**URL:** <https://www.ruby-forum.com/t/url-decode-rewrite-handling-vs-20/211414>\
**Category:** NGINX\
**Created:** [September 3, 2011, 3:22am UTC](https://www.ruby-forum.com/t/url-decode-rewrite-handling-vs-20/211414 "2011-09-03T03:22:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [September 3, 2011, 3:22am UTC](https://www.ruby-forum.com/t/url-decode-rewrite-handling-vs-20/211414/1 "2011-09-03T03:22:16Z")

</div>

Ubuntu Natty  
nginx/0.8.54  
PHP 5.3.5 / FPM / FastCGI

I’m just beginning to work with nginx for the first time. Converting my  
home server (very few hits) as an experiment for possible broader-scale  
testing and implementation on work servers (billions of hits per  
month).

Everything has gone beautifully… up until I begin working on an older  
app: Gallery 2. Some of the photos in my Gallery contain spaces, so  
they’re being encoded in the old-but-still-accepted + format. However,  
when I make the requests in nginx, it’s passing the pathinfo through to  
fastcgi as an escaped + (%2b) instead of a space (+ or %20).

The path is something like this:

[http://www.example.com/gallery/foo+bar.jpg.html](http://www.example.com/gallery/foo+bar.jpg.html)  
which gets rewritten to  
[http://www.example.com/gallery/main.php?g2\_path=foo+bar.jpg.html](http://www.example.com/gallery/main.php?g2_path=foo+bar.jpg.html)

In a path, + should be handled literally per RFC. In a query string,  
it’s supposed to be interpreted as a space per x-www-form-urlencoded.  
This is the disparity - what once was path is now query string. Because  
of the conversion, Apache translates the + from literal string to  
encoded space during rewrite. nginx simply encodes the literal string.

I don’t know if there’s a solid answer as to which one is strictly  
“correct”… but what I do need to know is if there’s a way to achieve  
the same behavior in nginx?

This is an interesting edge case…

Posted at Nginx Forum:

> **[URL decode rewrite handling: + vs. %20](https://forum.nginx.org/read.php?2,214668,214668#msg-214668)**
>
> Ubuntu Natty nginx/0.8.54 PHP 5.3.5 / FPM / FastCGI I'm just beginning to work with nginx for the first time. Converting my home server (very few hits) as an experiment for possible broader-scale testing and implementation on work servers (billions...

---

<div class="post-metadata">

**Author:** ![signe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@signe](https://www.ruby-forum.com/u/signe)\
**Post date:** [September 3, 2011, 12:22pm UTC](https://www.ruby-forum.com/t/url-decode-rewrite-handling-vs-20/211414/2 "2011-09-03T12:22:42Z")

</div>

edit the php script to do something like this before the rest of the  
script

$\_GET[‘g2\_path’] = urldecode($\_GET[‘g2\_path’])

?

---

<div class="post-metadata">

**Author:** ![signe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@signe](https://www.ruby-forum.com/u/signe)\
**Post date:** [September 3, 2011, 12:33pm UTC](https://www.ruby-forum.com/t/url-decode-rewrite-handling-vs-20/211414/3 "2011-09-03T12:33:11Z")

</div>

## splitice Wrote:

> edit the php script to do something like this  
> before the rest of the script
> 
> $\_GET[‘g2\_path’] = urldecode($\_GET[‘g2\_path’])
> 
> ?

That’s not the problem - query parameters are automatically decoded by  
PHP before processing even begins. nginx is turning a + (space) into a  
%2b (plus). Once it’s handed off to the application, it can’t tell the  
difference between a filename that actually had a +, and one that was  
converted by nginx. It needs to happen before it’s handed off to  
fastcgi.

I’ve already modified G2 to present URLs which are more accurately  
encoded, but that doesn’t resolve the issue since these URLs have been  
published for years. They’re in search engines, blog posts, etc.

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

---

<div class="post-metadata">

**Author:** ![signe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@signe](https://www.ruby-forum.com/u/signe)\
**Post date:** [September 5, 2011, 12:59pm UTC](https://www.ruby-forum.com/t/url-decode-rewrite-handling-vs-20/211414/4 "2011-09-05T12:59:31Z")

</div>

You can also use PCRE capturing inside virtual host configuration, to  
capture both SCRIPT\_FILENAME and PATH\_INFO without URL encoding:

location ~ ^(?\<SCRIPT\_FILENAME\>.+.php)$ {  
include fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME  
$document\_root$SCRIPT\_FILENAME;  
fastcgi\_pass …;  
}

location ~ ^(?\<SCRIPT\_FILENAME\>.+.php)(?\<PATH\_INFO\>.+)$ {  
include fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME  
$document\_root$SCRIPT\_FILENAME;  
fastcgi\_param PATH\_INFO $PATH\_INFO;  
fastcgi\_param PATH\_TRANSLATED  
$document\_root$PATH\_INFO;  
fastcgi\_pass phpfarm;  
}

Posted at Nginx Forum:

> **[Re: URL decode rewrite handling: + vs. %20](https://forum.nginx.org/read.php?2,214668,214792#msg-214792)**
>
> Ubuntu Natty nginx/0.8.54 PHP 5.3.5 / FPM / FastCGI I'm just beginning to work with nginx for the first time. Converting my home server (very few hits) as an experiment for possible broader-scale testing and implementation on work servers (billions...
