# Nginx rewrite

**URL:** <https://www.ruby-forum.com/t/nginx-rewrite/206479>\
**Category:** NGINX\
**Created:** [May 2, 2011, 7:54pm UTC](https://www.ruby-forum.com/t/nginx-rewrite/206479 "2011-05-02T19:54:57Z")\
**Posts on this page:** 6\
**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:** [May 2, 2011, 7:54pm UTC](https://www.ruby-forum.com/t/nginx-rewrite/206479/1 "2011-05-02T19:54:57Z")

</div>

Hello,  
I need to rewrite the URL

/img/14567.jpg to /img/14/14567.jpg

The image name is 14567.jpg, which is saved in /14 folder. /14 is  
caluculated by 14567 / 1000

How to implement is NGX rewrite?

Thanks,  
Yanxin

---

<div class="post-metadata">

**Author:** ![yanxin\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d6d6ee/32.png) [@yanxin\_z](https://www.ruby-forum.com/u/yanxin_z)\
**Post date:** [May 2, 2011, 8:12pm UTC](https://www.ruby-forum.com/t/nginx-rewrite/206479/2 "2011-05-02T20:12:08Z")

</div>

On Mon, May 02, 2011 at 07:54:57PM +0200, Yanxin Z. wrote:

> Hello,  
> I need to rewrite the URL
> 
> /img/14567.jpg to /img/14/14567.jpg
> 
> The image name is 14567.jpg, which is saved in /14 folder. /14 is  
> caluculated by 14567 / 1000
> 
> How to implement is NGX rewrite?

location /img/ {  
location ~ ^/img/(..)(.+)$ {  
alias /path/to/img/$1/$1$2;  
}  
}

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![yanxin\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d6d6ee/32.png) [@yanxin\_z](https://www.ruby-forum.com/u/yanxin_z)\
**Post date:** [May 2, 2011, 8:48pm UTC](https://www.ruby-forum.com/t/nginx-rewrite/206479/3 "2011-05-02T20:48:50Z")

</div>

Hello Igor S.,  
Thank you for your help.

You suggestion is great, but my problem is a little bit complicated.

The image name is flexible, can be from 0.jpg to 23456.jpg, we organize  
the image files into folders, whose name is the number divide by 1000.  
For example, the folder 100 saves the file 100000.jpg to 100999.jpg.

Both the folder and file name are flexible in my case.

Do you have any suggestion on it?

Thanks,  
Yanxin

---

<div class="post-metadata">

**Author:** ![yanxin\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d6d6ee/32.png) [@yanxin\_z](https://www.ruby-forum.com/u/yanxin_z)\
**Post date:** [May 2, 2011, 8:57pm UTC](https://www.ruby-forum.com/t/nginx-rewrite/206479/4 "2011-05-02T20:57:53Z")

</div>

On Mon, May 02, 2011 at 08:48:50PM +0200, Yanxin Z. wrote:

> Do you have any suggestion on it?

location /img/ {

```
location ~ ^/img/(\d)(\.jpg)$ {
    alias /path/to/img/0$1/$1$2;
}

location ~ ^/img/(\d\d)(\d*\.jpg)$ {
    alias /path/to/img/$1/$1$2;
}

return 404;

```

}

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![yanxin\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d6d6ee/32.png) [@yanxin\_z](https://www.ruby-forum.com/u/yanxin_z)\
**Post date:** [May 2, 2011, 9:29pm UTC](https://www.ruby-forum.com/t/nginx-rewrite/206479/5 "2011-05-02T21:29:55Z")

</div>

Thank you Maxim and Igor.  
It’s working for me.  
Yanxin

---

<div class="post-metadata">

**Author:** ![yanxin\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d6d6ee/32.png) [@yanxin\_z](https://www.ruby-forum.com/u/yanxin_z)\
**Post date:** [May 2, 2011, 9:19pm UTC](https://www.ruby-forum.com/t/nginx-rewrite/206479/6 "2011-05-02T21:19:31Z")

</div>

Hello!

On Mon, May 02, 2011 at 10:57:16PM +0400, Igor S. wrote:

> > Both the folder and file name are flexible in my case.  
> > alias /path/to/img/$1/$1$2;  
> > }
> 
> ```
> return 404;
> 
> ```
> 
> }

As far as I understand original question, the following should  
work (same as above, but fixed to use division by 1000 instead of  
two first digits):

location /img/ {

```
location ~ ^/img/(\d+)(\d\d\d\.jpg)$ {
    alias /path/to/img/$1/$1$2;
}

location ~ ^/img/(\d+\.jpg)$ {
    alias /path/to/img/0/$1;
}

return 404;

```

}

Maxim D.
