# Problems with some rewrite

**URL:** <https://www.ruby-forum.com/t/problems-with-some-rewrite/193778>\
**Category:** NGINX\
**Created:** [August 24, 2010, 9:16am UTC](https://www.ruby-forum.com/t/problems-with-some-rewrite/193778 "2010-08-24T09:16:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![revirii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d2c977/32.png) [@revirii](https://www.ruby-forum.com/u/revirii)\
**Post date:** [August 24, 2010, 9:16am UTC](https://www.ruby-forum.com/t/problems-with-some-rewrite/193778/1 "2010-08-24T09:16:36Z")

</div>

Greetings,

i’m currently facing a problem with some rewrites. Given an url i have  
to rewrite it to a local folder.

url: [www.domain.com/img/default/420006675369688559/image.png](http://www.domain.com/img/default/420006675369688559/image.png)  
doc root: /home/images/public/  
local path (under doc root): 420/006/420006675369688559/default.png

So the rewrite rule should take the number and split it, taking the 1st  
(420) and 2nd (006) three numbers out of the url and rewrite it to the  
local path. But the number isn’t always that long, it may also be only 1  
to 5 places long, e.g. 1, 12345 and 456 are possible. If the number is  
less than 6 places long, the rewrite should fill up the missing places  
with the character x.

Optional there’s a hyphen in front of the long number, e.g.  
/img/default/-420006675369688559/image.png

Brilliant concept, but i wasn’t asked… So far, so bad. So i wrote some  
rules, but as you might suggest, they don’t work. Here they are…

```auto
        # 1 number
        location "/img/default/-?([0-9] {1})/$" {
            rewrite "/img/default/([0-9-] {1})/(.*)\.(.*)$"
/$1xx/xxx/$1/default.$3;
        }
        # 2 numbers
        location "/img/default/-?([0-9] {2})/$" {
            rewrite "/img/default/([0-9-] {2})/(.*)\.(.*)$"
/$1x/xxx/$1/default.$3;
        }
        # 3 numbers
        location "/img/default/-?([0-9] {3})/$" {
            rewrite "/img/default/([0-9-] {3})/(.*)\.(.*)$"
/$1/xxx/$1/default.$3;
        }
        # 4 numbers
        location "/img/default/-?([0-9] {4})/$" {
            rewrite "/img/default/([0-9-] {3})([0-9] {1})/(.*)\.(.*)$"
/$1/$2xx/$1$2/default.$3;
        }
        # 5 numbers
        location "/img/default/-?([0-9] {5})+/$" {
            rewrite "/img/default/([0-9-] {3})([0-9] {2})/(.*)\.(.*)$"
/$1/$2x/$1$2/default.$3;
        }
        # 6 or more numbers
        location "/img/default/-?[0-9]+/$" {
            rewrite "/img/default/(-?[0-9]+ {3})(-?[0-9]+
{3})(-?[0-9]+)/(.*)\.(.*)$" /$1/$2/$3/default.$5 break;
        }
```

Maybe i don’t even need the locations… I’ve tried the last rule alone,  
with a rewrite to a website, but even that doesn’t work, so there must  
be some severe (logical) error in the location/rewrite rule.

Any help would be weclome 🙂

Posted at Nginx Forum:

> **[Problems with some rewrite](https://forum.nginx.org/read.php?2,123057,123057#msg-123057)**
>
> Greetings, i'm currently facing a problem with some rewrites. Given an url i have to rewrite it to a local folder. url: www.domain.com/img/default/420006675369688559/image.png doc root: /home/images/public/ local path (under doc root):...

---

<div class="post-metadata">

**Author:** ![revirii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d2c977/32.png) [@revirii](https://www.ruby-forum.com/u/revirii)\
**Post date:** [August 24, 2010, 10:46am UTC](https://www.ruby-forum.com/t/problems-with-some-rewrite/193778/2 "2010-08-24T10:46:46Z")

</div>

I’ve simplified the last rule a bit, to see where the problem might be.

```auto
rewrite "^/img/default/([0-9] {3})([0-9]
{3})/(.*)\.(gif|jpg|jpeg|png|tiff|bmp)$" http://www.google.com break;
->
rewrite "^/img/default/(.*)/(.*)\.(gif|jpg|jpeg|png|tiff|bmp)$"
http://www.google.com break;
```

So i replaced (-?[0-9] {3})(-?[0-9] {3})(-?[0-9]+) with (.\*), and now  
the rule works. So the problem is here: (-?[0-9] {3})(-?[0-9]  
{3})(-?[0-9]+)

I took/adapted this rule from  
[Module ngx\_http\_rewrite\_module](http://wiki.nginx.org/NginxHttpRewriteModule#rewrite) , but for some (to  
me unknown) reasons it isn’t working. Any idea?

well, nginx version: 0.6.32, debian lenny.

Posted at Nginx Forum:

> **[Re: Problems with some rewrite](https://forum.nginx.org/read.php?2,123057,123068#msg-123068)**
>
> Greetings, i'm currently facing a problem with some rewrites. Given an url i have to rewrite it to a local folder. url: www.domain.com/img/default/420006675369688559/image.png doc root: /home/images/public/ local path (under doc root):...

---

<div class="post-metadata">

**Author:** ![revirii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d2c977/32.png) [@revirii](https://www.ruby-forum.com/u/revirii)\
**Post date:** [August 24, 2010, 2:26pm UTC](https://www.ruby-forum.com/t/problems-with-some-rewrite/193778/3 "2010-08-24T14:26:55Z")

</div>

On Tue, Aug 24, 2010 at 03:15:46AM -0400, revirii wrote:

> (420) and 2nd (006) three numbers out of the url and rewrite it to the
> 
> ```
> }
> # 5 numbers
> 
> ```
> 
> Maybe i don’t even need the locations… I’ve tried the last rule alone,  
> with a rewrite to a website, but even that doesn’t work, so there must  
> be some severe (logical) error in the location/rewrite rule.
> 
> Any help would be weclome 🙂

You DO need the locations, but you DO NOT need the rewrites.  
Why do you use space in “([0-9] {2}” ?  
Here is example for 2 numbers location:

```
 # 2 numbers
 location ~ ^/img/default/-?(\d{2})/.+\.(.+)$ {
     alias /home/images/public/$1x/xxx/$1/default.$2;
 }

```

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![revirii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d2c977/32.png) [@revirii](https://www.ruby-forum.com/u/revirii)\
**Post date:** [August 24, 2010, 2:53pm UTC](https://www.ruby-forum.com/t/problems-with-some-rewrite/193778/4 "2010-08-24T14:53:51Z")

</div>

## Igor S. Wrote:

> You DO need the locations, but you DO NOT need the  
> rewrites.

ok.

> Why do you use space in “([0-9] {2}” ?

Because space was used here:

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

> Here is example for 2 numbers location:
> 
> ```
> # 2 numbers
> location ~ ^/img/default/-?(\d{2})/.+\.(.+)$
> 
> ```
> 
> {  
> alias  
> /home/images/public/$1x/xxx/$1/default.$2;  
> }

Hm, what is \d{2}? Is there any documentation? And:

```auto
nginx -t
2010/08/24 14:40:25 [emerg] 7386#0: pcre_compile() failed: missing ) in
"^/img/default/-?(\d"
2010/08/24 14:40:25 [emerg] 7386#0: the configuration file
/etc/nginx/nginx.conf test failed
```

I put it into “…”:

```auto
location ~ "^/img/default/-?(\d{2})/.+\.(.+)$"
```

But then i see:

```auto
2010/08/24 14:48:34 [emerg] 7449#0: the "alias" directive may not be
used inside location given by regular expression
2010/08/24 14:48:34 [emerg] 7449#0: the configuration file
/etc/nginx/nginx.conf test failed
```

Whats wrong now?

Thx in advance

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

---

<div class="post-metadata">

**Author:** ![revirii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d2c977/32.png) [@revirii](https://www.ruby-forum.com/u/revirii)\
**Post date:** [August 24, 2010, 4:11pm UTC](https://www.ruby-forum.com/t/problems-with-some-rewrite/193778/5 "2010-08-24T16:11:44Z")

</div>

## revirii Wrote:

> ## Igor S. Wrote:

> Hm, what is \d{2}? Is there any documentation?

hm, shame on my head. It’s for digit characters. I should work more with  
regular expressions…

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

---

<div class="post-metadata">

**Author:** ![revirii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d2c977/32.png) [@revirii](https://www.ruby-forum.com/u/revirii)\
**Post date:** [August 25, 2010, 12:01am UTC](https://www.ruby-forum.com/t/problems-with-some-rewrite/193778/6 "2010-08-25T00:01:18Z")

</div>

On Tue, Aug 24, 2010 at 08:52:55AM -0400, revirii wrote:

> [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/NginxHttpRewriteModule#rewrite)  
> I think this is a bug in markup.

> ```auto
> 
> ```
> 
> Whats wrong now?

Get 0.8.49 version. It’s stable enough. At least I use it on my job site  
[www.rambler.ru](http://www.rambler.ru).

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**
