I configured Magento 2 with two Store Views in order to have two front-end languages (English and Italian) and I would like to have the following behaviour:
http://example.com/ --> Store View "English" (code = "en") - Default
http://example.com/it/ --> Store View "Italian" (code = "it")
That is, the English Store View should be available on standard urls and the Italian Store View should be available within the "it" subfolder in the url. Nothing strange, a common configuration for multilanguage websites.
I found here a quite similar question regarding Magento 1.x: Different storeviews or websites in subfolders. But since the accepted answer is not trivial (it involves rewrites in the .htaccess
and modifications in the index.php
), I would like to ask if with Magento 2 there is a simpler way to get this done?
UPDATE
Premise: I don't mind about having multiple websites so I don't have the problem to having several websites with the same languages (that would be a problem for the below solution).
So I enabled the Add Store Code to Urls
option, from Configuration > Web > Url > Add Store Code to Urls, and it's working good. But now I have duplicated urls for the default language (English):
http://example.com/ --> Store View "English" (code = "en")
http://example.com/en/ --> Store View "English" (code = "en")
http://example.com/it/ --> Store View "Italian" (code = "it")
Is there a way to avoid this, i.e. avoid the subfolder in the url for the default language?
Best Answer
I ended up in using the below solution. I saw it is a quite common configuration for multi-language Magento websites.
This solution provides a default language with a redirect from "normal" url (urls without lang subfolder) to urls with the default lang subfolder.
For example, if
it
is the default language, it happen this:1) Create store views with lang code
2) Enable the Add Store Code to Urls option
Enabling this option I have the following:
3) Set a default language redirect in the .htaccess
Open the
.htaccess
and search for these lines:Then place the following lines just before the above ones:
These lines will redirect all urls without language subfolder to the same url with default subfolder
/it
at the beginning:Urls starting with
en/
,admin
,soap/
,rest/
are not redirected.You can customize it, changing the default language subfolder and adding other ignored urls (at least one for each language code you have).
4) Redirect the root to default language subdir
In the
.htaccess
search for these lines:Then place the following lines just after:
Drawbacks
I used this solution and it's working good for me. The only really drawback I have experienced is that the
.htaccess
is rewritten each time I update Magento, so I have to restore it after the updating.Another drawback can be if you have a multiple website configuration and several websites with the same languages as pointed out in a comment to this answer: https://magento.stackexchange.com/a/197/38275