String replace on rewrite module

jbruni nginx-forum at
Tue Jul 20 18:57:44 MSD 2010

Alex Sergeyev Wrote:
> You can also build recursive redirects like in my
> example, without break
> to make nginx repeat cycle of rewrites again, up
> to 10 times.

Thanks a lot, Alex,

Your comments enlightened my path!

Here is the working configuration:

	location ~ ^/(.*)/([^/]+)/image/([^/]*)$ {
		set $part1 $1;
		set $part2 $2;
		set $filename $3;
		rewrite ^.*$ /${part1}_$part2/image/$filename last;
	location ~ ^/([^/]+)/image/([^/]*)$ {
		set $folder $1;
		set $filename $2;
		rewrite ^.*$ /image/$folder/$filename break;

The first location directive is recursive, and keeps joining the paths,
replacing "/" by "_" on each iteration.

The second location directive is the final one. When all path pieces are
already joined, it does the final rewrite.


Anyway, a "string replace" function for the rewrite module remains a
wish as a [i]feature request[/i]...


Posted at Nginx Forum:,110833,110858#msg-110858

More information about the nginx mailing list