Forum: NGINX Help with redirect from old to new URL

Posted by kfawcett (Guest)
on 2012-09-09 15:55
(Received via mailing list)
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_virgin...

Here's the new URL:
http://www.mysite.com/property/mls-5028725-10425-v...

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: 
http://forum.nginx.org/read.php?2,230584,230584#msg-230584
Posted by Francis Daly (Guest)
on 2012-09-09 16:36
(Received via mailing list)
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 Daly        francis@daoine.org
Posted by kfawcett (Guest)
on 2012-09-09 19:51
(Received via mailing list)
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-v...

-Keith

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,230584,230588#msg-230588
Posted by "António P. P. Almeida" <appa@perusio.net> (Guest)
on 2012-09-09 20:07
(Received via mailing list)
On 9 Set 2012 15h54 CEST, nginx-forum@nginx.us wrote:

> 
http://www.mysite.com/idx/mls-5028725-10425_virgin...
>
> Here's the new URL:
> 
http://www.mysite.com/property/mls-5028725-10425-v...
>
> 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
Posted by Francis Daly (Guest)
on 2012-09-10 02:58
(Received via mailing list)
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-v...

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 Daly        francis@daoine.org
Posted by kfawcett (Guest)
on 2012-09-10 04:30
(Received via mailing list)
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: 
http://forum.nginx.org/read.php?2,230584,230596#msg-230596
Posted by Francis Daly (Guest)
on 2012-09-10 13:39
(Received via mailing list)
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 Daly        francis@daoine.org
Posted by kfawcett (Guest)
on 2012-09-10 15:18
(Received via mailing list)
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. :) Do you
accept Paypal donations?

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,230584,230618#msg-230618
Posted by Francis Daly (Guest)
on 2012-09-11 00:57
(Received via mailing list)
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. :)

"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 :-)

All the best,

  f
--
Francis Daly        francis@daoine.org
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.