How to do substitions (like perl s/// operator) in rewrites?

Maxim Dounin mdounin at mdounin.ru
Sun Jan 10 17:27:52 MSK 2010


Hello!

On Sun, Jan 10, 2010 at 09:08:48AM -0500, bartschipper wrote:

> I recently migrated a web site from Apache to Nginx (and from CMS-system x to CMS system y)
> Almost all of the rewrites are OK in the new CMS, except for one older class of article URLs:
> http://example.com/News/Articlepage-News/This-is-the-best-News-EVER.htm (example)
> 
> The direct mapping for these literal urls to the new urls is lost.
> I do however have a file (200k+ entries) that maps the titles to article-id's like so:
> 
> # old-class-urls.txt: (first two commented lines are not actually in the file)
> # all lowercase title without spaces and hyphens     article-id
> thisisthebestnewsever                                123456;
> ...

[...]

> map $uri $old-class-url {
>         include /etc/nginx/rewrites/old-class-urls.txt;
> }
> ...
> server {
> ...
>     location ~* ^/News/Articlepage-News/.*htm { 
>         rewrite ^/News/Articlepage-News/(.*)htm $1 ;
> ###
> ### change $uri to lowercase and remove the hyphens...
> ### I am looking for something equivalent like in perl:
> ### s/-//g;
> ### s/.*/\L{$1}/;

There is no easy way to do this without perl as of now.  With 
embedded perl it's trivial though.

Maxim Dounin



More information about the nginx mailing list