Help with redirect from old to new URL

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

Posted at Nginx Forum:

On Sun, Sep 09, 2012 at 09:54:56AM -0400, kfawcett wrote:

Hi there,

of the 40,000+ URLs are?
Within “location /idx/”, dynamically generate the new url, and issue a
redirect to that. “dynamically” means “use one of the embedded
languages,
or else talk to an external server”.

An incomplete example using fastcgi and php is:

file /tmp/idx-fixup:
“”"

<?php $old = $_SERVER[REQUEST_URI]; $new = str_replace('_', '-', $old); $new = substr_replace($new, '/property', 0, 4); header("Status: 301 Moved Permanently"); header("Location: $new"); ?>

“”"

fragment of nginx.conf:
“”"
location /idx/ {
include fastcgi.conf;
fastcgi_pass unix:php.sock;
fastcgi_param SCRIPT_FILENAME /tmp/idx-fixup;
}
“”"

Good luck with it,

f

Francis D. [email protected]

Thanks Francis!

It’s almost working, but once it runs I end up with this:
http://www.mysite.com/property, instead of
http://www.mysite.com/property/mls-5028725-10425-virginia-pine-lane-alpharetta-ga-30022

-Keith

Posted at Nginx Forum:

On 9 Set 2012 15h54 CEST, [email protected] wrote:

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: Lua | NGINX

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

Thank you Francis!

I had to put “fastcgi_param REQUEST_URI $request_uri;” in the location,
even
though it was already specified for the server.

Posted at Nginx Forum:

On Sun, Sep 09, 2012 at 01:50:46PM -0400, kfawcett wrote:

Hi there,

It’s almost working, but once it runs I end up with this:
http://www.mysite.com/property, instead of

http://www.mysite.com/property/mls-5028725-10425-virginia-pine-lane-alpharetta-ga-30022

What’s the output of

curl -i

on the starting url?

If it doesn’t have the correct /property/ url, then add the two lines

phpinfo(INFO_VARIABLES);
print(“\nold=$old\nnew=$new\n”);

immediately after the header() lines, and test again using curl. See
is the problem in “old” or in “new”, and debug accordingly. (Maybe your
fastcgi server doesn’t provide variables in the $_SERVER array.)

Note that the example was described as “incomplete”, partly because
the Location: header should have the full http://host/url. You can pick
appropriate variables and include them in the header() line.

f

Francis D. [email protected]

On Sun, Sep 09, 2012 at 10:29:55PM -0400, kfawcett wrote:

Hi there,

I had to put “fastcgi_param REQUEST_URI $request_uri;” in the location, even
though it was already specified for the server.

Because the location uses fastcgi_param, no server-level fastcgi_param
values are inherited. That’s why I had the “include” there.

But once you have a working system, that’s good enough.

Cheers,

f

Francis D. [email protected]

On Mon, Sep 10, 2012 at 09:17:48AM -0400, kfawcett wrote:

Hi there,

Thanks for the explanation. I didn’t have a fastcgi.conf and saw the params
in the server level, so I figured I was ok. Now I know better. :slight_smile:

“include fastcgi_params;” might have done mostly the same thing – I
guess
that versions and distributions differ in the default files provided.

Do you accept Paypal donations?

Thanks, but not for mailing list conversations :slight_smile:

All the best,

f

Francis D. [email protected]

Thanks for the explanation. I didn’t have a fastcgi.conf and saw the
params
in the server level, so I figured I was ok. Now I know better. :slight_smile: Do you
accept Paypal donations?

Posted at Nginx Forum: