Reputation: 2964
I have recreated a website and have lots of 301 to handle (from php urls to Rails urls).
It works perfectly with :
match "/produits/les-dallages/dallage-riva.html", :to => redirect("/produits/dallages/dalle-riva")
My problem is for this kind of old urls (providing from google webmaster tools):
"/produits/les-pavages/paves-carres/item/48-pav%C3%A9s-carr%C3%A9s.html"
The encoding is not understood because the url is transformed by the browser and Rails didn't understand the url with "é" instead of "%C3%A9"...
How to manage this kind of urls ?
Second question: how many routes (301) can I add in the routes.rb file ?
Thanks
Upvotes: 1
Views: 407
Reputation: 4766
In theory, you could add many routes you want. However, we should not put unnecessary in the routes file because it would eat up memory, and it needs to some times to process all the routes logic for every request before it can go to the controller.
In case you have quite a lot of urls to do redirection, rather than mess up with routes file, I would recommend you create a controller just to do redirection, because you could write a lot more flexible code. Maybe you could create a table to store from_url
(old url) and new_url
(for redirection). Then, inside a new controller, simply find the old url in the database and do a redirect.
class RedirectionController < ApplicationController
def index
redirect = Redirection.find_by_from_url(request.request_uri)
if redirect
redirect_to redirect.to_url, :status => :moved_permanently
else
render 'public/404', :status => :not_found, :layout => false
end
end
end
Last, use Route Globbing to match any urls for redirection. You can check out more about it on http://guides.rubyonrails.org/routing.html
match '/produits/*' => 'redirection#index'
For accent characters like 'é', you simply need to store this value inside your database. For MySQL, you should configure your database server to store utf-8
and update the connection inside database.yml.
encoding: utf8
collation: utf8_unicode_ci
You can try to redirect by the following code. It works perfectly okay. It must have # encoding: UTF-8
at the beginning of the file because there are those accent characters.
# encoding: UTF-8
class RedirectionController < ApplicationController
def index
redirect_to "produits/les-pavages/paves-carres/item/48-pavés-carrés"
end
end
Upvotes: 2