I have a similar question as Ruby on Rails. Unicode routes but the answer marked as working there does not work for me.
I want to have a basic route for a landing page that contains an umlaut (ä). It has to be exactly like that for SEO purposes.
# encoding: UTF-8
Udb::Application.routes.draw do
get "bonitätsauskunft" => "landing_pages#credit_reference", :as => :lp_credit_reference
When I enter http://localhost/bonitätsauskunft in my browser, I get a routing error No route matches "/bonit%c3%a4tsauskunft". So the query string is not unescaped before matching with the route, which I think is bad, because there are multiple ways to encode umlauts in URLs and I cannot know which one the browser uses.
for example CGI.escape("bonitätsauskunft") # => "bonit%C3%A4tsauskunft", note the capital C3 and A4 instead of c3 and a4 like Firefox sends.
So both get CGI.escape("bonitätsauskunft") and Rack::Utils.escape("bonitätsauskunft") do not match.
I also tried with no luck:
get ":page" => "landing_pages#credit_reference", :as => :lp_credit_reference, :page => /bonitätsauskunft/
get ":page" => "landing_pages#credit_reference", :as => :lp_credit_reference, :constraints => {:page => /bonitätsauskunft/}
The only thing that works for me is the awkward:
# encoding: UTF-8
class UmlautConstraint
def initialize(page)
@page = page
end
def matches?(request)
request.params[:page] == @page
end
end
Udb::Application.routes.draw do
get ":page" => "landing_pages#credit_reference", :constraints => UmlautConstraint.new("bonitätsauskunft")
get "bonitätsauskunft" => "landing_pages#credit_reference", :as => :lp_credit_reference
The second route, of course, is needed so I can use a named route to create links like link_to("Bonitätsauskunft", :lp_credit_reference) because Rails would not know how to satisfy the UmlautConstraint.
Isn’t there a normal and easy way to do this? I really thought rails 3 would be better with internationalization by now.
Rails 3.0.10, Ruby 1.9.2, Apache2 with passenger 3.0.7.
Use your first idea with
CGI.escape("bonitätsauskunft").downcaseIt will work with any browser. It don’t depend on browser encoding, but SERVER encoding. When you use another server (webrick), routes could need to be changed.
I’m using this type of “unicode” routes successfully for some time and it really has good SEO effect.