2012-07-15 26 views
1

有沒有一種方法可以爲每個區域/ tld使用不同的URL(特別是針對SEO的原因)?如何讓基於locale/tld的網址/路線?

所以說,如果我來通mysite.us我可以有:

http://mysite.com/some-nice-url

,如果來了直通mysite.org,我可以有:

http://mysite.fr/another-very-nice-url

謝謝

+0

重複的:http://stackoverflow.com/questions/2282346/i18n-locale-in-the-url – 2012-07-15 13:28:54

+0

沒有不是,請閱讀這個問題。我不想要一個/:locale/scope。 – Cezar 2012-07-15 13:40:19

回答

2

你可以做什麼是:

class ApplicationController < ActionController::Base 
    .. 
    before_filter :domain_locale 
    protected 
    def domain_locale 
     I18n.locale = request.host.split('.').last 
    end 

    ... 
end 

當然,您可以在domain_locale內添加一些更復雜的功能。

因此,對於你的routes.rb,你可以添加約束這樣的:

match 'soem_nice-url', :to => 'nice#some', :constraints => {:host => 'mysite.com'} 
match 'soem_nice-url', :to => 'nice#another', :constraints => {:host => 'mysite.fr'} 
+0

提取語言環境不是問題,每個語言環境的路由是...我想爲不同的語言環境提供不同的路由 – Cezar 2012-07-15 14:43:18

+0

啊我沒有意識到,更新了一些幫助的答案。 – moritz 2012-07-16 08:04:10