7

I am developing a Magento 2 store with two store views, one for each language(English and Italian). I turned on the "Add Store Code to Urls" option from backend, so my URLs looks like:

http://mystore.com/en (English)
http://mystore.com/it (Italian)

What I would like to do now is to remove the store code from URL for the default store view, to obtain something like this:

http://mystore.com/ (English)
http://mystore.com/it (Italian)

In Magento 1.9 there a few extensions that do this. I need to find something (or develop) for Magento 2.

Anyone has some hints?

A good example of what I am searching for is this: https://github.com/lalitmohann/magento-hide-default-store-code

1

4 Answers 4

1

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.

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

0
  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


See answer https://magento.stackexchange.com/a/287830/37969

-1

Turn off store code URLs in adminside

admin > stores > configuration > General > Web > Add Store Code to Urls > To set NO

After Run upgrade, static-content-deploy, and cache clean/flush commands.

1
  • Mind that the OP only wants to remove the store code for the default store.
    – Simon
    Commented Sep 16, 2020 at 14:44

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.