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 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.

Answer

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.

source: stackoverflow.com
js interview questions