Magento 2: How to Get Base URL?

Magento 2 How to Get Base URL

As a Magento developer, you are required to get a Magento 2 Base URL in various cases,

Today we’ll look at some instances of how to access the Base URL programmatically in Magento 2.

According to the DevDocs,

Each website in an Adobe Commerce or Magento Open Source installation has a base URL that is assigned to the storefront, and another URL that is assigned to the Admin.

Let’s see the implementation of all the possible methods below:

1) Get Base URL

2) Get Base URL without index.php

3) Get Base URL using Object Manager

4) Get Base URL in config.xml file

5) Get the Base URL in the layout .xml file

6) Get Media Base URL

7) Get the Store Link URL

8) Get the Base URL from the Root Directory

9) Get a Custom URL link in Block

10) Get Static Content URL in Block

That’s it!!

Feel free to share the post and use the Comments section below if you have any queries.

Thanks, See you in the next tutorial. Happy Coding !!

You may also like,

Magento 2: How to get a dynamic Base URL Path in XML?

How to remove/disable add to compare in Magento 2?

How to Show/Hide one field based on another field in UI Component forms in Magento 2?

Leave a Reply

Your email address will not be published. Required fields are marked *