How to remove parameters from the root URL if it does I18n

馋奶兔 提交于 2019-12-11 20:21:19

问题


Outdated (at least for Google)

Recently I studied SEO a bit and found out that search engines somehow "don't like" parameters in the URL. Comprehensible since they aren't really human readable or tend to not be permanent.


The problem now is, what if I followed the Rails Guides to set up I18n and then want to link to the root_path. It links to the root but adds a ?locale=en to the URL. So http://www.example.com/?locale=de.

Even if SEO isn't affected about this anymore, I think it looks much nicer if the URL looks like http://www.example.com/en instead of http://www.example.com/?locale=en.


回答1:


I found a pretty straightforward solution.

I just added another route to my routes.rb called i18n_root.

Example:

Rails.application.routes.draw do
  root 'welcome#index'

  # ...

  get ':locale' => 'welcome#index', as: :i18n_root
end

Now I just have to add the i18n_root_path to the links which link to the root path and everything works fine.

The returned URL:

http://www.example.com/en

Another possible solution, if I would have a lot of links already in the app, is to create a custom root.

Example:

Rails.application.routes.draw do
  # root 'welcome#index'  <= We don't want to have a default root

  # ...

  get ':locale' => 'welcome#index', as: :root
end

Hope this helps other people as well.



来源:https://stackoverflow.com/questions/34315715/how-to-remove-parameters-from-the-root-url-if-it-does-i18n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!