March 19, 2025Simon Walker

How to Get Base URL in Magento 2 With or Without Store Code?

blog
Table of Content
  • What is Magento 2 Base URL?
  • Why Change Magento 2 Base URL
  • Domain Change
  • SSL Configuration
  • Localisation
  • Developing a Development Site or Moving to Production
  • Get Magento 2 Base URL With Store Code
  • Get Magento 2 Base URL Without Store Code
  • How to Get Magento 2 Base URL in PHTML?
  • Method 1 - Get Magento 2 Base URL in PHTML
  • Method 2 – Using the Store Manager
  • Best Practices for Get Magento 2 Base URL
  • Use Dependency Injection for Get Magento Base URL
  • Follow SEO Principles
  • Use HTTPS for Base URLs
  • Clear Magento Cache After Get Magento 2 Base URL
  • Use Unique Codes for Each Store View
  • Final Thoughts on Get Magento 2 Base URL With and Without Store Code

Despite being one of the most widely used eCommerce platforms, Magento is not for beginners. It requires a certain level of technical and programming knowledge. But once you get comfortable with Magento 2, you won’t find any other eCommerce platform which comes close to it in terms of functionality, security, and performance. When we say programming or technical knowledge, you don’t need to be an expert coder.

Nevertheless, you do need to have basic technical skills to resolve frequently encountered issues in Magento 2. For example, one issue you must know how to deal with is Magento 2 get base URL. You may already be doing this in Magento 1. However, before you get overconfident, know that the method for get base URL Magento 2 is different than Magento 1.

Therefore, pay close attention as today we’ll discuss how to get base URL in Magento 2 without or without store code.

You also might like to see: How to get the Current & Base URL in Magento 2?

What is Magento 2 Base URL?

In technical terms, it the basic URL which points to your store’s root directory. In simple words, it is the starting point for all your store’s webpages. For example, the default Magento base URL looks like:

http://www.yourdomain.com/magento/

Store admins can configure the base URL as per their requirements.

Why Change Magento 2 Base URL

You may be wondering why you need to change the base URLs in the first place. Well, let’s discuss the why.

1. Domain Change

Suppose you are moving your existing store to a new domain. In that case, you’ll need to change the base URL to reflect the require changes. It will ensure that all internal links and redirects are valid. Otherwise, you’ll see a significant increase in 404 errors which can undermine your store’s online ranking, customer satisfaction, and brand reputation.

2. SSL Configuration

If you are adding an SSL Certificate to your store, changing base URL is mandatory. Of course, SSL is necessary if you wish to secure customer data.

3. Localisation

If you are targeting multiple regions simultaneously, it is better to target them with different URLs. This can be used to reflect local keywords and preferences. It can greatly benefit your SEO strategy. With improvements in ranking, you will see a noticeable improvement in conversion rates.

4. Developing a Development Site or Moving to Production

When your store is the development phase, it will be on a testing domain or localhost. Once the development part is complete, you’ll need to move it to the production side. This requires a change in the base URL.

Get Magento 2 Base URL With Store Code

Now, let’s discuss how to get Magento base URL:

To get the base URL with store code, use the following code:

$this->_storeManager->getStore()->getBaseUrl()

Where $this->_storeManager is an instance of

\Magento\Store\Model\StoreManagerInterface

The above code will give the following result.

http://www.example.com (If SEO rewrite is enabled)

And http://www.example.com/index.php (If SEO rewrite is not enabled)

Note:

To get the $this->_storeManager, call inject

\Magento\Store\Model\StoreManagerInterface $storeManager

at __construct( ) function at block class

just like :

public $_storeManager;

  public function __construct(

      \Magento\Store\Model\StoreManagerInterface $storeManager,

       .....

    ) {

       ...

  $this->_storeManager=$storeManager;

    }

If you want Base URL without index.php

$this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB)

Using Object Manager

  • Get Base URL in Magento 2
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');

$storeManager->getStore()->getBaseUrl();
  • Get Base URL Without Index.php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');

$storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);

Get Magento 2 Base URL Without Store Code

get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue("web/unsecure/base_url");?>

Related Articles:

How to Get Magento 2 Base URL in PHTML?

There are various ways to get Magento 2 base URL in phtml.

Method 1 - Get Magento 2 Base URL in PHTML

Enter the following command to get Magento 2 base URL in phtml:

getUrl(''); ?>

Method 2 – Using the Store Manager

  • Step 1: Before calling the base URL in phtml, you will need to fetch the base URL from a Block class. To do so, add a custom block file at:
app/code/FME/HelloWorld/Block/HelloWorld.php

namespace FME\HelloWorld\Block;

use Magento\Framework\View\Element\Template;

use Magento\Store\Model\StoreManagerInterface;

class CustomBlock extends Template

{

    protected $storeManager;

    public function __construct(

        Template\Context $context,

        StoreManagerInterface $storeManager,

        array $data = []

    ) {

        $this->storeManager = $storeManager;

        parent::__construct($context, $data);

    }

    public function getBaseUrl()

    {

        return $this->storeManager->getStore()->getBaseUrl();

    }

}
  • Step 2: Once complete, declare block in following layout XML:
app/code/FME/HelloWorld/view/frontend/layout/cms_page_view.xml

page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">

      referenceContainer name="content"

          block class="FME\HelloWorld\Block\CustomBlock" name="custom.block" template="FME_HelloWorld::custom.phtml"

      referenceContainer

page
  • Step 3: Go to app/code/FME/HelloWorld/view/frontend/templates/custom.phtml and add the following code:

// Get base URL

echo $block->getBaseUrl();

?>
  • Step 4: After completing the above steps, run the following commands:
php bin/magento cache:flush

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento cache:clean
  • Step 5: Go the frontend page where the block is created and you can see the base URL.

Best Practices for Get Magento 2 Base URL

1. Use Dependency Injection for Get Magento Base URL

This is the most important point to keep in mind when trying to get Magento base URL. Always inject StoreManagerInterface in your classes instead of using ObjectManager::getInstance(). Magento itself recommends against using the Object Manager since it undermines code maintainability. The Dependency Injection method ensures that the changes remain compatible with future Magento updates and ensures better performance.

2. Follow SEO Principles

Secondly, don’t ignore SEO principles at any point. After all, the entire purpose of running a Magento store is to earn profit and the best way do it is to improve search engine visibility. Therefore, ensure the URL rewrites are turned on and also avoid using the index.php in the base URL. Apart from it, use URL_TYPE_WEB constant since this helps remove unnecessary parts from the URL. We recommend testing your URLs regularly against best SEO practices.

3. Use HTTPS for Base URLs

One way to foster brand loyalty is to improve store security. We understand that store security encompasses a variety of things, but one key aspect is the HTTPS. Use HTTPS for the frontend and admins panels for better security. Given the importance of protecting user information, search engines like Google give preference to websites with HTTPS in search engine results pages.

4. Clear Magento Cache After Get Magento 2 Base URL

It is necessary to clear the cache after making changes to your store’s URL settings. This ensures the frontend reflects the latest changes.

5. Use Unique Codes for Each Store View

If you are running a multi store setup, then we recommend using a unique code for each store view. It makes it quite easy to manage the store and helps differentiate the content across different regions.

Final Thoughts on Get Magento 2 Base URL With and Without Store Code

That’s it for this tutorial. If you have any Magento related queries, then ask our expert Magento consultants. Remember, after making the changes, ensure that they are reflected on the website by clearing the cache. If you encounter any problems, revert the changes or ask our experts.

Read More Magento 2 Blogs:

This blog was created with FME's SEO-friendly blog