12

We run a magento 2 multistore in 2 langauges where default store view is german. Online shop is also available in french. Store code is added to URLs like the following:

www.domain.at
www.domain.at/de
www.domain.at/fr

When it comes to SEO we have the problem of duplicate content because the default store is available with AND without store code in URL. The following urls show the same content:

www.domain.at/de  
www.domain.at/

In fact we need same behavior like here for magento 1: Magento remove "default" store code from urls

Does anyone know how to fix this issue?

5 Answers 5

8
  1. Create new module in Vendor/HideDefaultStoreCode

registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_HideDefaultStoreCode',
    __DIR__
);

etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_HideDefaultStoreCode" setup_version="0.1.0" />
</config>
  1. Add an option to the admin panel

etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="web">
            <group id="url">
                <field id="hide_default_store_code" translate="label" type="select" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
                    <label>Hide Default Store Code</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

Default option value is No

etc/config.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <web>
            <url>
                <hide_default_store_code>0</hide_default_store_code>
            </url>
        </web>
    </default>
</config>
  1. Add helper

Helper/Data.php

<?php
namespace Vendor\HideDefaultStoreCode\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    const XML_PATH_HIDE_DEFAULT_STORE_CODE = 'web/url/hide_default_store_code';

    /**
     *
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     *
     * @param \Magento\Framework\App\Helper\Context $context
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        parent::__construct($context);
        $this->scopeConfig = $scopeConfig;
    }

    /**
     *
     * @return boolean
     */
    public function isHideDefaultStoreCode()
    {
        if ($this->scopeConfig->getValue(self::XML_PATH_HIDE_DEFAULT_STORE_CODE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
            return true;
        }
        return false;
    }
}
  1. Create after-plugin

etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Store\Model\Store">
        <plugin name="vendor_hide_default_store_code" type="\Vendor\HideDefaultStoreCode\Plugin\Model\HideDefaultStoreCode" sortOrder="0" />
    </type>
</config>

Plugin/Model/HideDefaultStoreCode.php

<?php

namespace Vendor\HideDefaultStoreCode\Plugin\Model;

class HideDefaultStoreCode
{
    /**
     *
     * @var \Vendor\HideDefaultStoreCode\Helper\Data 
     */
    protected $helper;

    /**
     *
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * 
     * @param \Vendor\HideDefaultStoreCode\Helper\Data $helper
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     */
    public function __construct(
        \Vendor\HideDefaultStoreCode\Helper\Data $helper,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ){
        $this->helper = $helper;
        $this->storeManager = $storeManager;
    }

    /**
     * 
     * @param \Magento\Store\Model\Store $subject
     * @param string $url
     * @return string
     */
    public function afterGetBaseUrl(\Magento\Store\Model\Store $subject, $url)
    {
        if ($this->helper->isHideDefaultStoreCode()) {
            $url = str_replace('/'.$this->storeManager->getDefaultStoreView()->getCode().'/','/', $url);
        }
        return $url;
    }
}

My Plugin for hide Default Store Code - https://github.com/alex-79/magento2-hide-default-store-code-from-url

1
  • great extension, it worked for me. searching for hours.
    – Amy
    Commented Sep 17, 2019 at 11:25
6

Preference \Magento\Store\Model\Store, override the following protected function.

protected function _updatePathUseStoreView($url)
{
    if ($this->isUseStoreInUrl()) {
        $url .= $this->getCode() . '/';
    }
    return $url;
}

with below code:

protected function _updatePathUseStoreView($url)
{
    if ($this->isUseStoreInUrl()) {
        if($this->getCode() == 'default'){
            $url .= '/';
        }else{
            $url .= $this->getCode() . '/';
        }

    }
    return $url;
}
2
  • Doesnt work with store switcher (store code pre-pended again)
    – George
    Commented Dec 14, 2017 at 15:18
  • use a plugin after the public function isUseStoreInUrl, its better than changing the di preference, more upgrade safe, thats why its there
    – DWils
    Commented Feb 23, 2018 at 23:20
3

You can disable store codes via admin at below path

Admin > Stores > Configuration > General > Web > URL options > Add Store Code to Urls > No

Keep cache Disabled or run upgrade/deploy/cache commands if you don't see the changes

3
  • 1
    We do not want to remove store codes from URL entirely. Only for default store view (= de)
    – christoph
    Commented Jul 24, 2017 at 9:57
  • can you pls help me for mageno 2.3 multi website magento.stackexchange.com/q/256694/57334 @Manoj Deswal
    – zus
    Commented Jan 4, 2019 at 9:45
  • good job, thanks for sharing Commented May 31, 2023 at 17:31
2

i have the same problem. After research here, i have an solution based on Renk´s answer. In backend set the "add store code". Than create a di.xml in your extension path under "Vendor/Module/etc/di.xml" for an plugin.

<?xml version="1.0"?>
   <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="\Magento\Store\Model\Store">
            <plugin name="RemoveDefaultStorePath" type="Vendor\Module\Plugin\RemoveDefaultStorePath" sortOrder="1" disabled="false" />
        </type>
    </config>

After that create the plugin class under "Vendor/Module/plugin/RemoveDefaultStorePath.php" with the follow function as "after" IsUseStoreInUrl to overwrite the standard behavior

<?php

namespace Vendor\Module\Plugin;


class RemoveDefaultStorePath
{
    public function afterIsUseStoreInUrl(\Magento\Store\Model\Store $subject, $resultIsUseInUrl)
    {
       if ($subject->getCode()==='default')
        {
          $resultIsUseInUrl = false;
          return $resultIsUseInUrl && 'default';
        }
        else
        {
          $resultIsUseInUrl = true;
          if(!$subject->getCode() ==='admin') {
            return $resultIsUseInUrl && $subject->getCode() . '/';
          } else {
            $resultIsUseInUrl = false;
            return $resultIsUseInUrl && $subject::ADMIN_CODE;
          }
       }
    }
}

And than compile and clean the cache.

php bin/magento setup:di:compile
php bin/magento cache:clean

I hope that´s help somebody - in my environment i have now the "default" store url´s without any addtional "store_code" and for the other multisites stores the preferd code "en"/"fr" etc. Please not: under the store config - don´t set the store code to url:

enter image description here

1

We created an extension for this issue. We used an after plugin on Magento\Store\Model\Store::isUseStoreInUrl to check if the store code is not the admin store and if the store is the default store. You can find the extension here : https://github.com/customgento/module-default-store-code-remover-m2

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.