How to change the root URL (move Magento 2 to another domain)?

sql
base-url
env.php
url
app-etc

(Dmitry Fedyuk) #1

Magento 2.0 similar to Magento 1.x stores shop root URL in the core_config_data database table.
The record paths are same: web/unsecure/base_url и web/secure/base_url.

So you can change root URL by SQL query:

UPDATE core_config_data 
SET value = 'http://example.ru/' 
WHERE path IN ('web/secure/base_url', 'web/unsecure/base_url');

It is sufficient for Magento 1.x but not for Magento 2.0.
Magento 2.0 for some strange purpose additionally stores root URL in the third path: design/head/includes

The record looks like:

<link  rel="stylesheet" type="text/css"  media="all" href="<root URL>/pub/media/styles.css" />

So you need one more SQL query:

UPDATE core_config_data
SET value = REPLACE(value, '<old root URL or domain>', '<new root URL or domain>')
WHERE path = 'design/head/includes';

For example:

UPDATE core_config_data
SET value = REPLACE(value, 'http://old.com/', 'http://new.com/')
WHERE path = 'design/head/includes';

Then delete the cache:

rm -rf var/cache/*

A bash script to automatic update a development environment from a production Magento 2 site
app/etc/env.php — the place where Magento 2 stores the database credentials, backend URL, enabled cache types, etc
How to find the Magento 2 frontend base URL using the command line?
A bash script to automatic update a production Magento 2 site from a development environment
(Dmitry Fedyuk) #2

See also:


(Dina F.) #3

But then the admin url bouced to a 404 page under my domain. I don’t know why. Could you help me?


(Dmitry Fedyuk) #4

Of course. You can buy my installation service here: Magento 2 installation service


(ElGatito) #5

@dmitry_fedyuk
Actually, regarding the second query, I would like to add that this one is no different than in the first Magento version.

Magento 2
This field is actually the custom Scripts and Style Sheets that can be found at the following location inside Magento 2 backend:

Content -> Design -> Configuration -> [Scope -> Action: Edit] -> HTML Head -> Scripts and Style Sheets

Magento
The location as been changed since Magento first of its name where it was located under:

System -> Configuration -> General -> Design -> HTML Head -> Miscellaneous Scripts

Official Magento Doc Reference.

Of course, it is a good idea to update it with the new path as some custom script/css tags with hardcoded urls in the header could be affected. The suggested query should take this in charge very well in most cases. One might also take a look at it manually as this is a custom field and the url may not be formatted exactly the same (i.e. http / https). It might even be better to use relative urls for head tags pointing to the actual website, this way there will be no need to bother about moving the site to a different domain.