I am trying to move a deployed WordPress Multi-Site to a local environment. I backed up the server database and files. In my local environment, I created a new database and imported the backup. I also copied the server files to my local machine.
I did these steps:
-
I changed wp-config.php and updated it with the new
db-name
,db-username
,db-password
and setdefine('DOMAIN_CURRENT_SITE', 'www.mysitename.com');
todefine('DOMAIN_CURRENT_SITE', 'localhost/mysitename');
. -
I changed
option_value
in the wp-option database table to point to my local address. -
I also deleted these lines from .htaccess:
# Redirect non-www urls to www RewriteCond %{HTTP_HOST} !^www.mysitename.com RewriteRule (.*) http://www.mysitename.com/$1 [R=301,L]
But when I go to 127.0.0.1/mysitename I get this error: This webpage has a redirect loop.
And when I change define('DOMAIN_CURRENT_SITE', 'localhost/mysitename');
to define('DOMAIN_CURRENT_SITE', 'localhost');
and I go to 127.0.0.1/mysitename address it shows me the phpinfo() page.
How can I fix my problem?
Please explain if I must set something in .htaccess and wp-config.php files.
Thank you so much.
Edit: Recently had to migrate a multi-site once again, and this guide still works fine as of 01/03-2016. The database edits are very figgedy, one mistake and you either get white screen of death or error connecting to database, so pay attention 🙂
I couldn’t figure out of to allow localhost links with protocol (http://) so if you see “(http:)localhost” read it as http:// localhost/. If someone could edit this for me that would be great!
Normal WordPress migration:
Edit wp-config.php, changed database name, username and password. Usually below “define(‘WP_DEBUG’.. I define “WP_HOME” and “WP_SITEURL”, these will override WordPress database settings for path to root.
“DOMAIN_CURRENT_SITE” is used for multisite installations. So if it isn’t a multisite, don’t use this, if it is a multisite, just use this and not WP_HOME and WP_SITEURL.
If you uploaded .htaccess from localhost aswell, delete it from the server.
And you’re done! No need to bother doing database changes. For your localhost installation, just define WP_HOME and WP_SITEURL to localhost instead.
How to migrate multisite:
Multisites in WordPress is a pain in the ass to migrate, it involves several database changes and even when you made everything correct it seems to only work some of the times without errors. However these are the recommended steps:
Edit your wp-config.php with the new database info, add the following defines:
You should have all of these in the wp-config.php of the site you are trying to export, so just copy-paste them from there, and edit DOMAIN_CURRENT_SITE.
Edit your htaccess like this (Rewritebase /path/ should be relative path, ie if your website is located at localhost/mypage it should be “RewriteBase /mypage/”):
Database edit time! These steps needs to be done for every subsite your multisite has, if I type change table “wp_site”, you should replicate it in wp_site1/wp_site2 etc…
In table wp_options: change fields “site_url” and “home” to (http:)localhost/subfolder (no trailing slash).
In table wp_blogs: change “domain” to “localhost”, and “path” for every entry for each subsite to /subfolder/ (must have trailing slash)
In table wp_site: change “domain” to “localhost”, and “path” to /subfolder/ (must have trailing slash)
In table wp_sitemeta: change “siteurl” to full path ie (http:)localhost/subfolder/ (must have trailing slash)
Now you should atleast have access to your page, if you still get redirection loop or white screen of death, double check the database changes. Now login to admin, and we need to change some hardcoded URLs in the database, I usually do this with a plugin called “Seach and replace”, so download and install this plugin, and use it by typing in what it should search for (old url) and what to replace it with (new url).
Now hopefully you have a functional migration of the multisite. As I noted I’ve done this a couple of times, and it almost seems random if everything works or not. I’d say I had success with this approach perhaps 6 out of 8 times.
Edit: Added how to migrate multisite.
This is close but a little different for a multisite that uses subdomains. You are required to edit your host file to show each site url as localhost(LINK):
Then, editing the database as stated above worked, ignoring any subdirectory changes beyond the .htaccess mentioned above but leaving the Base as /
In my install, I needed to update some across the headers and footers to get the styles to load.
Thanks to @ninja for pointing me in the right direction!
Ninja’s answer helped a lot. Though you can shorten the amount of the steps described in his answer by using wp-cli for database replacements, so you don’t need any third party plugins and to edit single database cells manually. wp-cli’s search-replace function works amazingly perfect, at least in my experience in case of usual wp sites. But in combination with the steps in ninja’s answer it also has worked with multisite in my case. Here is a great description on how to install wp-cli: https://wp-cli.org
To replace a string through out all the sites run:
Yes, the url strings are not wrapped with quotes. From my experience this way the replacement works perfect without any issues which might appear using quotes.
To replace strings in a particular site:
To see what urls are mapped to the sites:
Though the db replacements worked well. I had to deactivate multisite in the wp-config.php (by removing all the multisite constant definitions) and all the plugins first and then to reactivate them from network dashboard.
wp-cli search-replace docs:
https://developer.wordpress.org/cli/commands/search-replace/
You can follow the tutorial at this link.Its explained very nicely and is very easy to understand.
PS for step 6 i suggest you use Velvet Blues WordPress plugin