Bryan CS
Bryan CS

Reputation: 611

URL Rewrite Ruby on Rails

We have a Redmine installation on an old server and moved it to a new one with a new domain. Problem is, we need to redirect urls from the old domain to the new one. I really don't have much knowledge about ruby. I was thinking redirecting URLs on this one is as easy as some rewrite rules with .htaccess but I found it different. I've read some answers here redirect but can't figure out where to put those codes.

The scenario should be like:

from http://www.old-domain.com:3000/issues/3456

should be redirected to http://www.new-domain.com:3000/issues/3456

Can anyone help me how to do this? Or if you have better idea how to achieve this? I'm planning on reading some ruby guides for the meantime. Thanks guys!

Update:

I managed to create a simple redirect by doing the following: I created a controller redirect_controller.rb:

class RedirectController < ApplicationController
  before_filter :show

  def show
     redirect_to "http://www.new-domain.com:3000/", :status => :moved_permanently, :notice => "Notice: A redirect!!!"
  end
end

And added this to routes.rb:

map.connect '/', :controller => 'redirect'

But I only managed to redirect the page after a successful login. How can I redirect all pages to the new one retaining parameters such as /issues/3456 if there are any?

Upvotes: 0

Views: 1873

Answers (1)

epsilones
epsilones

Reputation: 11609

You can go to your application.rb file (I found it better than place the redirection in the application controller), which is loaded to start all the rails and all engines. The key here is to use

head :moved_permanently, :location => "http://www.newdomain.com/"

To call that you can wrap it in a method I found in a blog. I added some comment

def perm_redirect_to(options)
   url = case options
         when String # if you pass a string url, which is your case
           options
         else
           url_for(options) # if you pass some more complex option hash in 
                            # `options`, which doesn't seem to be your case
         end
   head :moved_permanently, :location => url
end

You can call this method passing your url perm_redirect_to(your_new_url)!

Upvotes: 1

Related Questions