# Help with redirect from old to new URL

**URL:** <https://www.ruby-forum.com/t/help-with-redirect-from-old-to-new-url/222754>\
**Category:** NGINX\
**Created:** [September 9, 2012, 3:55pm UTC](https://www.ruby-forum.com/t/help-with-redirect-from-old-to-new-url/222754 "2012-09-09T15:55:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 9, 2012, 3:55pm UTC](https://www.ruby-forum.com/t/help-with-redirect-from-old-to-new-url/222754/1 "2012-09-09T15:55:44Z")

</div>

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](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](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:

> **[Help with redirect from old to new URL](https://forum.nginx.org/read.php?2,230584,230584#msg-230584)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![kfawcett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@kfawcett](https://www.ruby-forum.com/u/kfawcett)\
**Post date:** [September 9, 2012, 4:36pm UTC](https://www.ruby-forum.com/t/help-with-redirect-from-old-to-new-url/222754/2 "2012-09-09T16:36:37Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kfawcett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@kfawcett](https://www.ruby-forum.com/u/kfawcett)\
**Post date:** [September 9, 2012, 7:51pm UTC](https://www.ruby-forum.com/t/help-with-redirect-from-old-to-new-url/222754/3 "2012-09-09T19:51:09Z")

</div>

Thanks Francis!

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

-Keith

Posted at Nginx Forum:

> **[Re: Help with redirect from old to new URL](https://forum.nginx.org/read.php?2,230584,230588#msg-230588)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![kfawcett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@kfawcett](https://www.ruby-forum.com/u/kfawcett)\
**Post date:** [September 9, 2012, 8:07pm UTC](https://www.ruby-forum.com/t/help-with-redirect-from-old-to-new-url/222754/4 "2012-09-09T20:07:31Z")

</div>

On 9 Set 2012 15h54 CEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

[http://www.mysite.com/idx/mls-5028725-10425\_virginia\_pine\_lane\_alpharetta\_ga\_30022](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](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: [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](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

---

<div class="post-metadata">

**Author:** ![kfawcett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@kfawcett](https://www.ruby-forum.com/u/kfawcett)\
**Post date:** [September 10, 2012, 4:30am UTC](https://www.ruby-forum.com/t/help-with-redirect-from-old-to-new-url/222754/5 "2012-09-10T04:30:29Z")

</div>

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:

> **[Re: Help with redirect from old to new URL](https://forum.nginx.org/read.php?2,230584,230596#msg-230596)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![kfawcett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@kfawcett](https://www.ruby-forum.com/u/kfawcett)\
**Post date:** [September 10, 2012, 2:58am UTC](https://www.ruby-forum.com/t/help-with-redirect-from-old-to-new-url/222754/6 "2012-09-10T02:58:10Z")

</div>

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](http://www.mysite.com/property), instead of

[http://www.mysite.com/property/mls-5028725-10425-virginia-pine-lane-alpharetta-ga-30022](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](http://host/url). You can pick  
appropriate variables and include them in the header() line.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kfawcett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@kfawcett](https://www.ruby-forum.com/u/kfawcett)\
**Post date:** [September 10, 2012, 1:39pm UTC](https://www.ruby-forum.com/t/help-with-redirect-from-old-to-new-url/222754/7 "2012-09-10T13:39:12Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kfawcett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@kfawcett](https://www.ruby-forum.com/u/kfawcett)\
**Post date:** [September 11, 2012, 12:57am UTC](https://www.ruby-forum.com/t/help-with-redirect-from-old-to-new-url/222754/8 "2012-09-11T00:57:42Z")

</div>

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 D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![kfawcett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@kfawcett](https://www.ruby-forum.com/u/kfawcett)\
**Post date:** [September 10, 2012, 3:18pm UTC](https://www.ruby-forum.com/t/help-with-redirect-from-old-to-new-url/222754/9 "2012-09-10T15:18:32Z")

</div>

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:

> **[Re: Help with redirect from old to new URL](https://forum.nginx.org/read.php?2,230584,230618#msg-230618)**
>
> 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...
