How to remove parameters from the root URL if it does I18n
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
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
I found a pretty straightforward solution.
I just added another route to my
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:
Another possible solution, if I would have a lot of links already in the app, is to create a custom root.
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.