# Making a 301 redirector for \*everything\*

**URL:** <https://www.ruby-forum.com/t/making-a-301-redirector-for-everything/69179>\
**Category:** Rails\
**Created:** [September 19, 2006, 5:45am UTC](https://www.ruby-forum.com/t/making-a-301-redirector-for-everything/69179 "2006-09-19T05:45:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [September 19, 2006, 5:45am UTC](https://www.ruby-forum.com/t/making-a-301-redirector-for-everything/69179/1 "2006-09-19T05:45:59Z")

</div>

I want to move my site elsewhere. So I thought I could throw in a  
Webrick instance to serve as a 301 redirector on the old port and send  
all the traffic to the new site. I’ve got the headers[“status”]=“301  
moved permanently” and the redirect\_to “[http://www.newsite.com](http://www.newsite.com)” but  
what’s happening is that the basic URL ([mysite.com/blog](http://mysite.com/blog)) is being  
redirected (my controller is /blog), but more complex urls like  
/blog/2005/09/07/abc.html are getting a “recognition failed” error.  
Makes sense since there is no “2005” action.

Is there a trick I need to do in order to send every url from the old  
site to the new one? While I’m in there, since my url pattern will be  
staying the same, am I going to be able to keep that information as part  
of the redirect or do I have to redirect to the main page of my new  
site? In other words can I go from [oldsite.com/blog/2005/09/07/abc.html](http://oldsite.com/blog/2005/09/07/abc.html)  
to [newsite.com/2005/09/07/abc.html](http://newsite.com/2005/09/07/abc.html) ?

Thanks!

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [September 19, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/making-a-301-redirector-for-everything/69179/2 "2006-09-19T22:59:11Z")

</div>

> Is there a trick I need to do in order to send every url from the old  
> site to the new one? While I’m in there, since my url pattern will be  
> staying the same, am I going to be able to keep that information as  
> part  
> of the redirect or do I have to redirect to the main page of my new  
> site? In other words can I go from  
> [oldsite.com/blog/2005/09/07/abc.html](http://oldsite.com/blog/2005/09/07/abc.html)  
> to [newsite.com/2005/09/07/abc.html](http://newsite.com/2005/09/07/abc.html) ?
> 
> Thanks!

# Static content control

map.connect ‘/protected/\*path’, :controller =\> ‘protected’,  
:action =\> ‘render\_static’

I use this to force any /protected URL to controller protected, action  
static. I suspect you could (not tested)

Map.connect ‘/\*path’, :controller =\> ‘redirect’, action =\> ‘redirect’

This map should put the whole URL in to params[:path], and call  
controller redirect, method redirect for every one. You should be able  
to construct an appropriate redirect from that.

Be sure to clear out /public so that rails gets the hits.

A simpler way might be to configure a redirect in your web server.  
Apache and lighty can do this.

Regards,  
Rich

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [September 19, 2006, 11:59pm UTC](https://www.ruby-forum.com/t/making-a-301-redirector-for-everything/69179/3 "2006-09-19T23:59:18Z")

</div>

If you’re using Apache, all you need is this in [oldsite.com](http://oldsite.com)’s root  
.htaccess:

Redirect permanent /blog [http://www.newsite.com](http://www.newsite.com)

This will catch all the stuff under /blog and pass it along to newsite.

Should be doable in Lighttpd as well, but I’m stuck on Apache at the  
moment…

On 9/18/06, Duane M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
Matt J.  
removed\_email\_address@domain.invalid  
President/Technical Director, Acme Art Company ([acmeartco.org](http://acmeartco.org))

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [January 14, 2008, 1:10am UTC](https://www.ruby-forum.com/t/making-a-301-redirector-for-everything/69179/4 "2008-01-14T01:10:59Z")

</div>

Where do you put this configuration line?

On Sep 18 2006, 7:45 pm, Duane M. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [September 20, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/making-a-301-redirector-for-everything/69179/5 "2006-09-20T19:05:16Z")

</div>

> > Thanks!
> 
> A simpler way might be to configure a redirect in your web server.  
> Apache and lighty can do this.

Agreed. If you have apache, this is a 3-line .htaccess file in your  
webroot:

RewriteEngine on  
RewriteCond %{HTTP\_HOST} [oldsite.com](http://oldsite.com)  
RewriteRule ^(.\*) [http://www.newsite.com$1](http://www.newsite.com$1)

An even simpler way is to re-point [oldsite.com](http://oldsite.com) to [newsite.com](http://newsite.com) in DNS,  
unless you specifically wanted the 301 step inbetween.

- Mark.
