How to configure base URLs for a Magento 2 website?

How to configure base URLs for a Magento 2 website?

In this article, we are going to change the base URLs of a Magento 2 website, using the swiss army knife for Magento developers, sysadmins and devops i.e. n98-magerun2.

Base URLs can be also changed from the Magento Site backend as well as from the Database using phpMyadmin or Mysql REPL too.

Here, we are going to change base URLs of our Magento 2 website form https://from.magehost.cloud/ to https://to.magehost.cloud/     

Magento 2, just like magento 1 stores all the crucial configuration in the Database in core_config_data table.

Before configuring the Magento site, make sure web-server (vHost) is configured for the domain/sub-domain and correct DNS records are set.

In our cPanel Server Stack, to add a domain/sub-domain, perform the following steps:
  1. Login to the cPanel account and search for "Aliases" and click on it.
  2. To add a domain alias, enter the domain name in the text box.
  3. Click Add Domain.
  4. No redirections are required in this case, so click on "Disable Redirection" from the "Manage Redirection" option for the domain alias.
In cPanel Server Stack, there is already n98-magerun2 present on the server, and can be accessed with the command "magento2-cli", yet n98-magerun2 can be easily downloaded via. (wget https://files.magerun.net/n98-magerun2.phar)

Note : Use n98-magerun2/magento2-cli from the document root of the magento 2 site, as the cPanel account user.

To run a magento site on a domain/sub-domain we need to set the required base URLs in the core_config_data table.

Run following command to get the list of already set base URLs 
  1. magento2-cli config:store:get '%base%url%';
The output will be somewhat similar to 



To change the base URL run the commands as shown below
  1. magento2-cli config:store:set web/secure/base_url https://to.magehost.cloud/
  2. magento2-cli config:store:set web/unsecure/base_url https://to.magehost.cloud/
Note : Don't forget to clear the caches after making the changes.

Clear the caches with
  1. magento2-cli cache:clear
  2. magento2-cli cache:flush

Re-check the base URLs after making changes, the output (magento2-cli config:store:get '%base%url%') will be like








Make sure of the trailing slash '/' at the end of base URLs

Depending on the use cases, there could exist multiple base URLs with different "scope"s and their respective "scope-id"s

In short, There are three scope types - default, websites & stores.
  1. If scope is set to default then scope_id is always 0.

  2. If scope is set to websites then scope_id is website_id.

  3. If scope is set to stores then scope_id is store_id(store view).

Refer :- magento2-cli config:store:set --help
  1. Usage:

    config:store:set [options] [--] <path> <value>

  2. Arguments:  

    path                                           The store config path like "general/local/code"
    value                                         The config value

  3. Options:
         
    --scope[=SCOPE]                            The config value's scope (default, websites, stores) [default: "default"]
    --scope-id[=SCOPE-ID]                   The config value's scope ID [default: "0"]

  4. Help:

    Set a store config value by path.
    To set a value of a specify store view you must set the "scope" and "scope-id" option.

So now we easily changed the base URLs (both secure and unsecure) from "https://from.magehost.cloud/" to "https://to.magehost.cloud/" from the command line itself without needing to log into backend or without any MySQL DB query.

    • Related Articles

    • How to configure base URLs for a Magento 1 website?

      In this article, we are going to change the base URLs of a Magento 1 website, using the swiss army knife for Magento developers, sysadmins and devops i.e. n98-magerun. Base URLs can be also changed from the Magento Site backend as well as from the ...
    • Magento Customer login does not work in Chrome browser

      Problem description Customers can not log in into Magento with Chrome browser, the login page just reloads without any errors or warnings. But when customer logins in other browsers and admin back-end login function properly. Cause Magento sets an ...
    • How to disable a website in virtualmin?

      To disable a website using virtualmin, follow the following steps :  Step 1:  Click on the Virtualmin section Step 2:  Select the Disable and Delete option Step 3:  Click on the Disable Virtual Server module Step 4:  Enter in a reason for disabling ...
    • How to enable a disabled website in virtualmin?

      Follow the steps below : Step 1:  Click on the Virtualmin section Step 2:  Select the disabled server from the drop down menu Step 3:  Select the Disable and Delete option Step 4:  Click on the Enable Virtual Server module Step 5:  Select if you’d ...
    • Using traceroute to troubleshoot network issues

      When you connect to a website, the traffic has to go through several intermediaries like your local router, your Internet service provider’s routers, onto larger networks, and so on. Traceroute shows us the path traffic takes to reach the website. It ...