# Redirection rule 301 on routes.rb

**URL:** <https://www.ruby-forum.com/t/redirection-rule-301-on-routes-rb/184309>\
**Category:** Rails\
**Created:** [February 28, 2010, 5:44pm UTC](https://www.ruby-forum.com/t/redirection-rule-301-on-routes-rb/184309 "2010-02-28T17:44:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aldoitalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@aldoitalo](https://www.ruby-forum.com/u/aldoitalo)\
**Post date:** [February 28, 2010, 5:44pm UTC](https://www.ruby-forum.com/t/redirection-rule-301-on-routes-rb/184309/1 "2010-02-28T17:44:46Z")

</div>

hi, i have created a redirection controller for redirect old pages of  
another previous site:

####### start code ######  
class RedirectController \< ApplicationController  
def index  
headers[“Status”] = “301 Moved Permanently”  
if params[:url]  
redirect\_to params[:url]  
return  
else  
redirect\_to index\_path  
end  
end  
end  
####### end code ######

in the routes.rb i have setting this rules:

####### start code ######  
map.connect ‘/index.htm’, :controller =\> ‘redirect’  
map.connect ‘/modules.php?name=News&file=pdf&sid=87’, :controller =\>  
‘redirect’  
####### end code ######

for the first rule (index.htm) the redirection woks fine, but on second  
rule the redirection don’t success.  
what if the correct syntax i doing to use for my purpose?  
thanks

---

<div class="post-metadata">

**Author:** ![aldoitalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@aldoitalo](https://www.ruby-forum.com/u/aldoitalo)\
**Post date:** [March 1, 2010, 11:13am UTC](https://www.ruby-forum.com/t/redirection-rule-301-on-routes-rb/184309/2 "2010-03-01T11:13:01Z")

</div>

> map.connect ‘/modules.php?name=News&file=pdf&sid=87’, :controller =\>  
> ‘redirect’
> 
> for the first rule (index.htm) the redirection woks fine, but on second  
> rule the redirection don’t success.  
> what if the correct syntax i doing to use for my purpose?

map.connect ‘/modules.php’, :controller =\> ‘redirect’

The ? params are automatically extracted from the URL to the params hash  
in  
Rails, leaving the URL being the part before the ?.

If you want to redirect only specific parameters on that URL, you’ll  
have to  
handle that within your RedirectController (or alternatively handle this  
with a Rails metal that internally rewrites the URL and returns a 404  
(to  
bounce it up in to Rails)).

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![aldoitalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@aldoitalo](https://www.ruby-forum.com/u/aldoitalo)\
**Post date:** [March 1, 2010, 1:05pm UTC](https://www.ruby-forum.com/t/redirection-rule-301-on-routes-rb/184309/3 "2010-03-01T13:05:26Z")

</div>

thank you Andy

i have tryed with : map.connect ‘/modules.php’, :controller =\>  
‘redirect’

but it not wook

i have try with metal too:

####### start code ######  
require(File.dirname( **FILE** ) + “/…/…/config/environment”) unless  
defined?(Rails)

class RedirectOldSite  
def self.call(env)  
if env[“PATH\_INFO”] =~ /^/modules.php?name=News&file=pdf&sid=(\d\*)/  
[200, {“Content-Type” =\> “text/html”}, [“Hello, World!”]]  
else  
[404, {“Content-Type” =\> “text/html”}, [“Not Found”]]  
end  
end  
end  
####### end code ######

but it not work too.

i suppose the problem is the .php extension, because if i use the rule:  
/^/modules/  
and in the browser i point to mydomain/modules  
the rule setting in metal works fine.

any idea?

---

<div class="post-metadata">

**Author:** ![aldoitalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@aldoitalo](https://www.ruby-forum.com/u/aldoitalo)\
**Post date:** [March 1, 2010, 4:11pm UTC](https://www.ruby-forum.com/t/redirection-rule-301-on-routes-rb/184309/4 "2010-03-01T16:11:32Z")

</div>

> if env[“PATH\_INFO”] =~ /^/modules.php?name=News&file=pdf&sid=(\d\*)/

PATH\_INFO already has the query string stripped off. This works for me  
in a  
quick Rails Metal:

```
if env["REQUEST_URI"] =~ /^\/test\.php\?foo=bar/
  [200, {"Content-Type" => "text/html"}, ["Hello, World!"]]
else
  [404, {"Content-Type" => "text/html"}, ["Not Found"]]
end

```

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![aldoitalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@aldoitalo](https://www.ruby-forum.com/u/aldoitalo)\
**Post date:** [March 1, 2010, 5:38pm UTC](https://www.ruby-forum.com/t/redirection-rule-301-on-routes-rb/184309/5 "2010-03-01T17:38:56Z")

</div>

i have try this solution, but still don’t work
