Magento base url: Why and how to change it in Magento 2

Sometimes we may not notice the URL of each website, however, this factor plays an important role in SEO of the website. That’s why many the store owners have to change the Magento base URL overtime for the purpose of SEO best practices. In this article, we will provide you 3 useful methods to change the Magento 2 base URL. Enjoy the quick tutorial with a few clicks!

Why you should change Magento Base URL in Magento 2?

There are many reasons why you need to change store URLs in Magento website. For example, you can use this method when you need to change the domain URL or when you need:

  • Need to develop a staging site
  • Change the server setup
  • Want a development site
  • Need to have a subdomain site
  • Simply want to change the domain

In all these cases, follow the below method to change the base URL in Magento 2.  With the below method, the base URL can be changed easily and all the sub-URLs will be redirected with the new base URL. Besides, we will go through steps by steps how to properly change a Magento 2 website’s URL (without 404 error on admin or any pages).

Methods to change Magento Base URL

Methods 1:  Change the Magento 2 Base URL from the Backend

Configure the unsecure base URLs

  1. Go to your Magento 2 backend:  Stores > Settings > Configuration.
  2. On the left menu, under General section, you click Web.
  3. Expand Base URLs section. Enter the unsecure (http) Base URL for your Magento website. Remember to end the URL with a forward slash (“/”).
  4. Optionally If you don’t want to use a secure base URL for the Admin, do the following steps:
    a. Expand Base URLs (Secure) section.
    b. Enter your unsecure (http) base URL into Secure Base URL field. The URL should be the same in both the Base URLs and Base URLs (Secure) sections.
  5. After completing, click the Save Config button.

Configure the secure base URLs

If your domain has a valid security certificate, you can change the URLs for either the storefront and Admin – or both – to run over a secure (https) channel.

configure-magento-base-url1

In your Magento 2 backend, move to Stores > Settings > Configuration.

 At the left menu, under General section, click Web.

Expand Base URLs (Secure) section.

configure-magento-base-url2
  1. Then, do the following steps:
  2. To use a secure (https) for the storefront, enter the Secure Base URL, followed by a forward slash (“/”).
  3. If you plan to to run the entire storefront over a secure (https) channel, set Use Secure URLs on Storefront to Yes.
  4. If you need to run the entire Admin over a secure (https) channel, set Use Secure URLs in Admin to Yes.
  5. When complete, you click the Save Config button.

Method 2: Change base URL using Magento 2 CLI command

Besides, you can also change base URL using Magento 2’s CLI command.

  • Log in to your server using SSH and change directory to Magento 2 root folder, for instance, you install Magento 2 website in /var/www/html
cd /var/www/html
  • Use the following command to update base url
php bin/magento setup:store-config:set --base-url="http://yourwebsite.com/"
  • Use the below command to update base url (secure)
php bin/magento setup:store-config:set --base-url-secure="https://yourwebsite.com/"
  • Keep in mind to put a slash at the end of each URL
  • Finally, flush cache to update changes
php bin/magento cache:flush

Method 3: Change the Magento 2 Base URL by the Command Line

The following Magento 2’s CLI command will update the Magento base URL and the base-url-secure values.

1. Access your Magento hosting server via SSH

2. Run the following command

mysql -u $database_user -p $database_name

3. Enter the password when prompted

4. Run the below command to access the database, where the database is the name of the database use database

5. Use the following command. The current base_urls set in Magento will be displayed in here:

select * from core_config_data where path like '%base%url%';

6. To modify the Magento base URLs, run the following commands

update core_config_data set value = 'http://example.com/' where path = 'web/unsecure/base_url';
update core_config_data set value = 'https://example.com/' where path = 'web/secure/base_url';

These are the  simply  3 methods to change Magento 2 Base URL. We hope this tutorial was useful for you.  If you have any troubles, please contact to our Magento development services and feel free to contact us at any time.

Finally

magesolution

I hope this tutorial can help you change the Magento 2 Base URL. If you have any questions or any issues, don’t hesitate to leave us a comment. And if you want to find an agency which is able to assist you in modify your Magento shop, Magesolution is the best choice for you. We have 15+ years of experience in building and customizing online store, thus, with our Magento Customization Services, we are confident that your Magento store will reach new heights. Feel free to contact us now!