Magento 2 tutorials

How to change base URLs in Magento 2 (4 methods)

Pinterest LinkedIn Tumblr

There are many reasons why you need to change store URLs. For examples: Moving from localhost to live site, changing new website’s domain, moving from HTTP to HTTPS, or changing subdomain <=> domain…

In this tutorial, 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)

There are several ways to changes base URLs of your Magento 2 website.

Method 1: Changing base URL via Dashboard

Magento 2 allows users to change store’s base URL via backend dashboard. To do that follow these step:

Step 1: Login to admin and go to Store => Configuration => Web

change baseurl via dashboard

Step 2: Expand Base URL section and insert your desired new base URL, for example:

change base url

Remember to put a slash (/) at the end of the URL so that it can be extended with additional URL Keys.

Step 3: Save changes and go to Base URL (secure) section

The different between Base URL and Base URL (secure) is that if you plan to run your store in HTTPS you need to configure Base URL (Secure)

I strongly recommend running Magento 2 in HTTPS because it will make your store look secured and trusted. Also, you need https for further online payment configuration.

You can purchase an SSL certificate from SSL providers like Comodo, or use free SSL by Let’s Encrypt – the nonprofit Internet Security Research Group (ISRG).

In this section, input your Secure Base URL (same with base URL but use HTTPs instead of HTTP)

change base url secure

If you want to force using HTTPS on storefront, select Yes in Use secure urls on storefront and Enable HTTP Strict Transport Security (HSTS)

use secure urls on storefront

use secure urls on storefront

If you want to use HTTPS for Admin URL as well, select YES in Use Secure URLs in Admin. (If you select this option, you will need to use https url in order to access Magento 2 admin section)

url secure in admin

Finally, click save config to apply change. Clear cache and reload your website to see if base URL is changed.

Method 2: PHPmyadmin

Personally, I prefer this method because it works directly with Magento 2 database

Step 1: Open Phpmyadmin and login with your Magento 2 database credentials (in case you forget the login credentials, open app/etc/env.php file to retrive.

For example:

'dbname' => 'magento',
'username' => 'magento',
'password' => 'password',

Then the login for phpmyadmin is: magento / password

Step 2: Select the database associate with your Magento 2 website and search for table core_config_data, find the following lines:

  • web/unsecure/base_url
  • web/secure/base_url

Click edit and change value to your new base URL (remember to put slash at the end of every URL and put https in web/secure/base_url)

edit table in phpmyadmin

Finally, click GO to save changes and clear cache to update URL

Quick tip: you can also excute SQL command directly in PHPMYADMIN to update baseurl, use this syntax:

UPDATE core_config_data SET value = 'http://yourmagento2website.com/' WHERE path LIKE 'web/unsecure/base_url';
UPDATE core_config_data SET value = 'https://yourmagento2website/' WHERE path LIKE 'web/secure/base_url';

Put these lines in SQL tab of the selected database in PHPMYADMIN as below
run command in phpmyadmin

Change http://yourmagento2website.com/ and https://yourmagento2website.com/ with your desired URL and click on GO to update.

Method 3: Command line

If you are familiar with SSH, you can use SSH to update URL with a few commands.

Step 1: Login to your server using SSH

Step 2: Access mysql using this command

mysql -u $database_user -p$password

Replace $database_user with your mysql user and $password with your database password

For example, if you have root account of mysql with password as: 123456

mysql -uroot -p123456

Step 3: access your Magento 2 website database using this line

use $database

With $database = your Magento 2 store’s database.

Sample output:

use database

Next, run these command to update base URL (same as the commands we use in PHPmyadmin)

 update core_config_data set value = ‘http://yourwebsite.com/‘ where path = ‘web/unsecure/base_url’;
update core_config_data set value = ‘https://yourwebsite.com/‘ where path = ‘web/secure/base_url’;

Method 4: Change baseurl using Magento 2 CLI command

Alternative, you can also change baseurl using Magento 2’s CLI command

Access to your server using SSH and change directory to Magento 2 root folder, for example you install Magento 2 website in /var/www/html

cd /var/www/html

Run the following Magento 2 command to update base url

php bin/magento setup:store-config:set --base-url="http://yourwebsite.com/"

Run the following Magento 2 command to update base url (secure)

php bin/magento setup:store-config:set --base-url-secure="https://yourwebsite.com/"

Remember to put a slash at the end of each URL

Finally, flush cache to update changes

php bin/magento cache:flush

Conclusion

All the methods I mentioned in this tutorial will bring the same results, choose the method that you feel most comforable with.

If you encounter any error during the process, please drop a line in comment section (with a screenshot if possible). I will try my best to help you resolve the issue

Hung Tran is the main editor of magentip.com, he loves to write about everything related to Magento 2

Write A Comment