I will address setting the default language as root page using WPML. The first part will explain how the plugin handles multiple languages for a bit, if you are just interested in the solution scroll to the bottom of this article.
I have been building bi-lingual websites for a while, mainly with the help of the WPML plugin. It’s an awesome plugin that saves me a lot of the hassle required to build a multi-lingual websites.
WPML provides three ways to display your website’s languages:
- Each language in it’s own directory ex: http://example.com/en (recommended)
- Different domain per language ex: http://en.example.com
- Add language name as a URL parameter ex: http://example.com/?lang=en
I have always used (and recommend) the first option.
It gives you the option to use or not use a directory for the default language, let’s say we have an English and Arabic website and English is the default language, you can have it like http://example.com/en and http://example.com/ar.
Or you choose not to use directory for the home page so it’s just the domain for the default language http://example.com/. But I prefer to have a directory for the default language as well, that way it’s more convenient for the user.
Having a directory for each language raises the question of what to show in the root domain, without the language directory. WPML gives us the two options:
- A root page.
- Static HTML file.
The root page
This is a regular WordPress page that WPML will display when a user visits the root URL of your website, you may use it as a landing page that makes the user choose what languages does he/she want’s to visit.
Static HTML file
The same as the root page but using an HTML file on the server, instead of a WordPress page.
default language as root page
I wanted to have a directory for each language but at the same time, didn’t want to display a root page nor a static html file for the root URL. Instead, I wanted the root URL to redirect to the default language directory so when a user visits http://example.com it automatically redirects to http://example.com/en
There is no way to do so through WPML settings but we are able to do so using a custom WPML/Wordpress function, just copy the following snippet and paste it anywhere in your functions.php file
Now visit the root of your website and it should redirect to the default language.
If you have anything to add, let us know in the comments.