Nginx redirects from old to new URL

We switch providers to search for properties, and each of them formats the URLs a little differently. We have already registered 40,000 URLs, and users need to redirect 301 new URLs.

The only difference in the URLs is the transition 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 on how to redirect all of these URLs without knowing what each of the 40,000 URLs is?

Thanks Kit

+2
source share
2

- perl, . nginx perl, . , :

nginx.conf http:

perl_modules  perl/lib;
perl_set $fix_uri 'sub {
        use File::Basename;
        my $req = shift;
        my $uri = $req->uri;
        $uri = basename($uri);
        # Do some magic here, probably more elegant than this
        $uri =~ s/idx/property/g;
        $uri =~ s/_/-/g;
        return $uri;
}';

:

   location ~ "/idx/(.*" {
            set $redirect_path $fix_uri;
            rewrite . $redirect_path;
    }
0

ngx_lua.

location /idx/ {
    rewrite_by_lua '
        local tempURI, n = ngx.re.gsub(ngx.var.uri, "_", "-")
        local newURI, m = ngx.re.sub(tempURI, "/idx", "/property", "i")
        return ngx.redirect(newURI, ngx.HTTP_MOVED_PERMANENTLY)
    ';
}

(gsub) "_" "-"

(sub) "/idx" "/property"

0

All Articles