Migrating from Drupal to Rails - Routing
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.
开发者_Python百科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
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
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.
精彩评论