String replace on rewrite module

jbruni nginx-forum at nginx.us
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:


[code]
	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;
	}
[/code]


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.

Cool!


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

Thanks!

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,110833,110858#msg-110858




More information about the nginx mailing list