views:

192

answers:

2

I've developed a new Ruby on Rails site for my organization. I want the new Rails site to intercept incoming requests that were meant for the old site and display a message for the user indicating that the new site is launched, a link the new URL they were most likely trying to get to, and a reminder to update bookmarks.

So I'm pretty green when in comes to rails routing so I'm asking how would you do this. Is there a 'rails way'?

Here are my thoughts so far.

The old site does not use clean urls so every request goes to the default route which in the new site is the home controller with a query string. I was thinking in the controller I can test to see if params[:q] is set and then depending on what the q parameter is, search for and render the an info page directing the user to the new link. If the q parameter doesn't make sense (I don't care about catching every page on the old site, just the important ones) redirect to a custom 404 page informing the user that the link was probably for the old site, and give the user a search page.

Any thoughts, is there a better way?

I appreciate any input.

Thanks

+1  A: 

Is there a consistent way that the URLs have changed? Such as /?q=pie becoming /dessert/pie ?

If not, and it requires some sort of manual db query, you'll have to do it the way you mentioned. I would create a redirect action that catches all the paths to keep it separate from your home/index action.

Also, if you care about SEO juice, make sure to use a 301 redirect:

redirect_to dessert_url, :status=>301

You would then have to do an instant redirect (no intermediate page saying 'update your bookmark'). What I would do is the instant redirect, and put a flash[:notice] message saying to "please update your bookmarks".

Please post some examples of URLs if you need more direct examples.

lamplighter
raytiley
***Adding to Above comment*** The new URL that the above resource would map to would be /shows/100The IDs of the resources are conserved between the two sites
raytiley
Great. hakunin gave a more concrete example. If the old URLs ever have a path in addition to a query string (ie. /some/path?q=1), you would have to put the before_filter in your application_controller.rb. Then you don't even have to worry about your routes.rb catching the old URLs.
lamplighter
+1  A: 

In your Rails controller responsible for the homepage (let's say it's HomeController) add before_filter like so:

class HomeController < ActionController::Base
  before_filter :handle_drupal_requests, :only => :index

Then add the handler method itself handle_drupal_requests like so.

class HomeController < ActionController::Base
  before_filter :handle_drupal_requests, :only => :index

  # ... other code ...

private
  def handle_drupal_requests
     if params[:q].present?
        flash[:notice] = "You're being redirected because blah."
        redirect_to convert_drupal_url(params[:q]), :status => 301
     end
  end

  def convert_drupal_url(query_string)
     # your logic for converting query string, for example:
     item, id = query_string.split('&').last.split('=')
     item_controller = item.underscore.split('_').first.pluralize
     {:controller => item_controller, :action => "show", :id => id}
  end
end
hakunin
Thanks...This works great. Sorry about the delayed response, I moved on to some other issues before coming back to this.
raytiley