# Rewrite rule failing with unicode urls on nginx with pcre 8.3

**URL:** <https://www.ruby-forum.com/t/rewrite-rule-failing-with-unicode-urls-on-nginx-with-pcre-8-3/218081>\
**Category:** NGINX\
**Created:** [March 20, 2012, 5:07pm UTC](https://www.ruby-forum.com/t/rewrite-rule-failing-with-unicode-urls-on-nginx-with-pcre-8-3/218081 "2012-03-20T17:07:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [March 20, 2012, 5:07pm UTC](https://www.ruby-forum.com/t/rewrite-rule-failing-with-unicode-urls-on-nginx-with-pcre-8-3/218081/1 "2012-03-20T17:07:20Z")

</div>

First post on the list, so please be gentle 🙂

I asked this question on stackoverflow, but wasn’t getting any love  
there, and then realised I should go to the source!

I managed to get nginx (1.0.14) compiled with the latest PCRE (8.30),  
changed the rewrite rule to use UTF8, but it still fails in some cases  
with unicode characters.

My rewrite rule is

```
location / {
    try_files $uri $uri/ /index.php;
    rewrite "(*UTF8)^/imgthumb/(.*)$" /timthumb.php?$1 last;
}

```

This works fine with images without unicode, but fails when the filename  
contains unicode characters.

so  
`/imgthumb/src=/wp-content/uploads/8姉妹の古いマトリョーシカ.jpg&h=121&w=137&zc=1`  
**fails**

but `/imgthumb/src=/wp-content/uploads/MOD0005.jpg&h=121&w=137&zc=1`  
**works** fine.

On Apache using .htaccess rewrite rule, it works with both

`RewriteRule ^/imgthumb/(.*)$ /timthumb.php?$1 [L]`

I noticed that the problem seems to stem from the fact that the PHP  
script gets only one parameter (src) into the `$_GET` array with nginx,  
but with apache rewrite it’s broken down to different parameters…

Is my nginx rewrite rule wrong? Is there a way to make this work?

Posted at Nginx Forum:

> **[rewrite rule failing with unicode urls on nginx with pcre 8.3](https://forum.nginx.org/read.php?2,224105,224105#msg-224105)**
>
> First post on the list, so please be gentle :) I asked this question on stackoverflow, but wasn't getting any love there, and then realised I should go to the source! I managed to get nginx (1.0.14) compiled with the latest PCRE (8.30), changed the...

---

<div class="post-metadata">

**Author:** ![yoavaner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/ccd318/32.png) [@yoavaner](https://www.ruby-forum.com/u/yoavaner)\
**Post date:** [March 21, 2012, 5:59pm UTC](https://www.ruby-forum.com/t/rewrite-rule-failing-with-unicode-urls-on-nginx-with-pcre-8-3/218081/2 "2012-03-21T17:59:25Z")

</div>

Hello!

On Tue, Mar 20, 2012 at 12:06:36PM -0400, yoavaner wrote:

> **fails**  
> but with apache rewrite it’s broken down to different parameters…  
> This a problem in rewrite handling, it inconsistently escapes $1  
> in some cases but not in others. And it’s not really related to  
> unicode, just any escaped char in uri will be enough to trigger  
> escaping.

This needs fixing.

> Is my nginx rewrite rule wrong? Is there a way to make this work?

Avoid using rewrites.

Maxim D.

---

<div class="post-metadata">

**Author:** ![yoavaner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/ccd318/32.png) [@yoavaner](https://www.ruby-forum.com/u/yoavaner)\
**Post date:** [March 21, 2012, 6:03pm UTC](https://www.ruby-forum.com/t/rewrite-rule-failing-with-unicode-urls-on-nginx-with-pcre-8-3/218081/3 "2012-03-21T18:03:49Z")

</div>

Thanks Maxim!

> This a problem in rewrite handling, it inconsistently escapes $1  
> in some cases but not in others. And it’s not really related to  
> unicode, just any escaped char in uri will be enough to trigger  
> escaping.

> This needs fixing.

Good to know. Thanks.

> > Is my nginx rewrite rule wrong? Is there a way to make this work?

> Avoid using rewrites.

Or avoid using nginx, but that won’t be an ideal solution obviously.  
Hope that this gets fixed some time…

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

---

<div class="post-metadata">

**Author:** ![yoavaner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/ccd318/32.png) [@yoavaner](https://www.ruby-forum.com/u/yoavaner)\
**Post date:** [March 21, 2012, 6:03pm UTC](https://www.ruby-forum.com/t/rewrite-rule-failing-with-unicode-urls-on-nginx-with-pcre-8-3/218081/4 "2012-03-21T18:03:51Z")

</div>

On Wednesday 21 March 2012 14:56:49 yoavaner wrote:  
[…]

> > > Is my nginx rewrite rule wrong? Is there a way to make this work?
> > 
> > Avoid using rewrites.
> 
> Or avoid using nginx, but that won’t be an ideal solution obviously.  
> Hope that this gets fixed some time…

Try this:

location ~ (_UTF8)^/imgthumb/(._)$ {  
fastcgi\_pass … or whatever you use  
fastcgi\_param QUERY\_STRING $1;  
…  
}

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![yoavaner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/ccd318/32.png) [@yoavaner](https://www.ruby-forum.com/u/yoavaner)\
**Post date:** [March 21, 2012, 10:40pm UTC](https://www.ruby-forum.com/t/rewrite-rule-failing-with-unicode-urls-on-nginx-with-pcre-8-3/218081/5 "2012-03-21T22:40:15Z")

</div>

## Valentin V. Bartenev Wrote:

> > Hope that this gets fixed some time…  
> > wbr, Valentin V. Bartenev
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

Thanks Valentin. Your suggestion sounds promising. However, I’m sorry,  
but I’m not that familiar with the nginx configuration rules. How do I  
point it in the location directive to my php script (which is  
timthumb.php) ?

I tried something like this, but it doesn’t seem to work… I’m getting  
an empty response unfortunately.

```
    location ~ (*UTF8)^/imgthumb/(.*)$ {
            fastcgi_pass unix:/var/spool/phpfpm.sock;
            fastcgi_param SCRIPT_FILENAME

```

$document\_root/timthumb.php;  
fastcgi\_param SCRIPT\_NAME /timthumb-orig.php;  
fastcgi\_param QUERY\_STRING $1;  
}

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

---

<div class="post-metadata">

**Author:** ![yoavaner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/ccd318/32.png) [@yoavaner](https://www.ruby-forum.com/u/yoavaner)\
**Post date:** [March 21, 2012, 11:04pm UTC](https://www.ruby-forum.com/t/rewrite-rule-failing-with-unicode-urls-on-nginx-with-pcre-8-3/218081/6 "2012-03-21T23:04:35Z")

</div>

Never mind. My mistake. I forgot to use `include fastcgi_params`

This seems to work !! Fantastic. Thanks so much for the suggestion  
Valentin.

Posted at Nginx Forum:

> **[Re: rewrite rule failing with unicode urls on nginx with pcre 8.3](https://forum.nginx.org/read.php?2,224105,224205#msg-224205)**
>
> First post on the list, so please be gentle :) I asked this question on stackoverflow, but wasn't getting any love there, and then realised I should go to the source! I managed to get nginx (1.0.14) compiled with the latest PCRE (8.30), changed the...
