Nginx rewrite

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

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.

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

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.

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

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.