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

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

Despite being one of the most widely used eCommerce platforms, Magento is not easy to use. It requires a fair bit of programming knowledge to use. We are not saying that you need to be an expert coder but need basic coding skills to manage frequently encountered issues. 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 from Magento 1. Therefore, pay close attention as today we’ll discuss how to get the base URL in Magento 2 without or without store code.

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

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.
  • 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.

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

  • Localization
  • 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.

  • 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

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 enable)

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

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 Base URL Without Store Code in Magento 2

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

That’s it for this tutorial. If you have any Magento related queries, then ask our expert Magento consultants.


Related Articles:

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