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
One way to do it is using a perl subroutine to change the underscores to hyphens. You need to compile nginx with perl unless it isn't included already. This isn't a fully working solution, but it might get you going in the right direction:
In nginx.conf add this within the http section:
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;
}';
Then in the location you may call the subroutine:
location ~ "/idx/(.*" {
set $redirect_path $fix_uri;
rewrite . $redirect_path;
}
I prefer the ngx_lua module myself.
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)
';
}
First line (gsub) changes all "_" to "-"
Second line (sub) changes first "/idx" to "/property"
Third line is obvious