2

In Magento 2 I have two stores with "add store code in url" option enable.

Store code 1 = "fr"

Store code 2 = "es"

I want to automatically add default store code in url:

Redirect http://mywebsite.com to http://mywebsite.com/fr/

Actually when I visit http://mywebsite.com I stay to this url and default store code is not added automatically.

Do you know how to change that ? Is there a Magento 2 option for that ? Or should I write condition in .htaccess file ?


Solution :

I add this rule in my .htaccess file to solve this issue :

RewriteCond %{HTTP_HOST} mywebsite\.com$
RewriteRule ^$ /fr/ [R=301,L]
3
  • Navigate to Stores > Configuration > General > Web and find the Url Options section. There you choose to Add Store Code to URLs selecting Yes from the corresponding select box and Save Config
    – Mehran
    Jul 26, 2022 at 10:12
  • @Mehran yes this option is already enable, I mentioned it in my post
    – Fbr
    Jul 26, 2022 at 10:24
  • @Mehran My problem is when I visit url without store code, store code is not automatically added to the url. But when I visit url with store code it's OK
    – Fbr
    Jul 26, 2022 at 10:29

3 Answers 3

0

for this you have to go store->configuration->select the scope(select store) and On the left panel, under General, select Web-> Base URL like this http://127.0.0.1/magento2/pub/fr/ or http://127.0.0.1/magento2/pub/es/. http://127.0.0.1/magento2/pub/ is the default base url

3
  • Thanks but it's not working because it duplicate store code in url : mywebsite.com/fr/fr My problem is when I visit url without store code, store code is not automatically added to the url. But when I visit url with store code it's OK
    – Fbr
    Jul 26, 2022 at 12:15
  • how you solved this problem??I'm also facing this issue Nov 11, 2022 at 12:21
  • @aich.khalid check my answer above.
    – AboElnouR
    Dec 10, 2023 at 15:17
0

Some options:

  1. Specify in your env.php file the data you want applied and run bin/magento setup:upgrade in terminal.

  2. SSH into your server and run this bin/magento command:

    bin/magento setup:store-config:set --base-url="http://<your_url>" --base-url-secure="https://<your_url>" --use-secure=0 --use-secure-admin=0

  3. Set your config values in the core_config_data table of your database. In phpmyadmin, run this sql: and edit as you see fit per scope:

    SELECT * FROM core_config_data WHERE path like '%web%'

0

I just did it and here is my solution:

First, go to Stores -> Configuration -> General -> Web -> Base URLs

And please do all the following exactly step by step:

  1. Change Base URL to http://mywebsite.com/fr/
  2. For Base Link URL Uncheck Use system value and keep the value http://mywebsite.com/
  3. Change Base URL for Static View Files to http://mywebsite.com/static/
  4. Change Base URL for User Media Files to http://mywebsite.com/media/

Now your website will be working for the default store view

Caution: Other store views now won't be working, fortunately, we can fix it.

  1. Switch the configuration scope for every store view and change the Base URL to http://mywebsite.com/[store_code]/

Cheers, now your website is working as desired.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.