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:
example.com/ --> example.com/it/
example.com/contact --> example.com/it/contact
...
1) Create store views with lang code
- Go to Stores > All Stores.
- Create a Store View for each language.
- Set in the Code field the language code.
- Example: I have two store views, Italiano with code "it" (default view) and English with code en.
2) Enable the Add Store Code to Urls option
3) Set a default language redirect in the .htaccess
Open the .htaccess
and search for these lines:
############################################
## never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
Then place the following lines just before the above ones:
############################################
## redirects for urls without lang dir to default lang (it)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^/(it|en|admin\w*|soap|rest)($|/.*$)
RewriteRule ^(.*)$ /it/$1 [L,R=301]
These lines will redirect all urls without language subfolder to the same url with default subfolder /it
at the beginning:
example.com/contact --> example.com/it/contact
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:
############################################
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
Then place the following lines just after:
############################################
## redirect root to default lang (it)
RedirectMatch 301 ^/$ /it/
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
I had the same requirement and was able to solve it easily with just one plugin for Magento\Store\Model\Store::isUseStoreInUrl
method.
public function afterIsUseStoreInUrl(\Magento\Store\Model\Store $subject, $resultIsUseInUrl)
{
if ($subject->getCode() != $subject::ADMIN_CODE && $subject->isDefault())
{
return $resultIsUseInUrl && $this->scopeConfig->getValue('web/url/use_store_in_default');
}
else
{
return $resultIsUseInUrl;
}
}
I registered the plugin in global di.xml
instead of frontend because the first time this method is called area has not yet been set so the plugin would not get executed.
Best Answer
When Magento sites add a second store on the same domain there is a problem with legacy requests to old URLs. This is bad SEO and a bad experience for customers too I just Resolved this problem by this extension
https://github.com/lalitmohann/Magento-Store-Codes
Extension available on github. click here
i solved this Problem , i hope that's save others Person here is the details :
1- Download the zip existe in this page : https://github.com/lalitmohann/Magento-Store-Codes
2- unzip the file and put the folder in {app/code/community/}
3- In your backoffice go to "System > Tools > Compilation" and click the rebuild button
4- always in backoffice got to "System > Configuration > Web > URL options" and set the attribute "and default store view" to No and save the configuration
5- Clear your cach magento and enjoy your application :) .