Help with redirect from old to new URL

António P. P. Almeida appa at perusio.net
Sun Sep 9 17:17:49 UTC 2012


On 9 Set 2012 15h54 CEST, nginx-forum at nginx.us wrote:

> Hi,
>
> We are switching vendors for our property searches and each one
> formats the URLs a little bit differently. We already have 40,000+
> URLs indexed and want users to be 301 redirected to the new URL.
>
> The only difference in the URLs is a switch from underscores to
> hyphens, and from /idx/ to /property/.
>
> Here is the old URL:
> http://www.mysite.com/idx/mls-5028725-10425_virginia_pine_lane_alpharetta_ga_30022
>
> Here's the new URL:
> http://www.mysite.com/property/mls-5028725-10425-virginia-pine-lane-alpharetta-ga-30022
>
> Any ideas how to redirect all of these URLs without knowing what
> every one of the 40,000+ URLs are?
>
> Thanks,
> Keith

Use Lua: http://wiki.nginx.org/HttpLuaModule#ngx.re.gsub

Try:

location /idx {
    set_by_lua $new_uri 'local newstr, n =
        ngx.re.gsub(ngx.var.uri, "_", "-") 
        newstr, n = ngx.re.sub(newstr, "/idx/", "/property/")
        return newstr';

    return 301 $scheme://$host$new_uri; 
}

Assuming you want to do a 301 redirect.

--- appa



More information about the nginx mailing list