WordPress: Move Multisite from server to localhost

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:

Read More
  1. I changed wp-config.php and updated it with the new db-name, db-username, db-password and set define('DOMAIN_CURRENT_SITE', 'www.mysitename.com'); to define('DOMAIN_CURRENT_SITE', 'localhost/mysitename');.

  2. I changed option_value in the wp-option database table to point to my local address.

  3. 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.

Related posts

Leave a Reply

4 comments

  1. 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:

    1. Backup your database from localhost.
    2. Create new database on new server, import the exported database here.
    3. Upload all WordPress-files to the server.
    4. 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.

      define('WP_HOME', 'http://mysitename.com');
      define('WP_SITEURL', 'http://mysitename.com');
      
    5. If you uploaded .htaccess from localhost aswell, delete it from the server.

    6. Visit http://mysitename.com/wp-admin, login and go to permalinks settings and press save to generate a new .htaccess.

    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:

    1. Export database, same as above.
    2. Upload new database, same as above.
    3. Add all your WordPress files, same as above.
    4. Edit your wp-config.php with the new database info, add the following defines:

      define('MULTISITE', true);
      define('SUBDOMAIN_INSTALL', false); // true or false depending if the paths for each multisite is pointed by a subdomain.
      define('DOMAIN_CURRENT_SITE', 'localhost'); // localhost is the DOMAIN of the new location, so even if the ABS_PATH is localhost/whatever, you should have localhost here.
      define('PATH_CURRENT_SITE', '/');
      define('SITE_ID_CURRENT_SITE', 1);
      define('BLOG_ID_CURRENT_SITE', 1);
      

    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.

    1. 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/”):

      # MultiSite
      RewriteEngine On
      RewriteBase /subfolder-from-localhost/
      RewriteRule ^index.php$ - [L]
      
      # add a trailing slash to /wp-admin
      RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
      
      RewriteCond %{REQUEST_FILENAME} -f [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^ - [L]
      RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
      RewriteRule ^([_0-9a-zA-Z-]+/)?(.*.php)$ $2 [L]
      RewriteRule . index.php [L]
      
    2. 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)

    1. 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).

      Search for: http://mysitename.com
      Replace with: (http:)localhost
      
    2. 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.

  2. 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):

    C:WindowsSystem32driversetc

    127.0.0.1 localhost
    ::1 localhost
    127.0.0.1   localhost.com
    127.0.0.1   subdom1.localhost.com
    127.0.0.1   subdom2.localhost.com 
    etc..
    

    Then, editing the database as stated above worked, ignoring any subdirectory changes beyond the .htaccess mentioned above but leaving the Base as /

     # MultiSite
    
     RewriteEngine On
    
     RewriteBase /
    
     RewriteRule ^index.php$ - [L]
    

    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!

  3. 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:

    wp search-replace //oldurl.com //dev.url.test --network
    

    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:

    wp search-replace //oldurl.com //dev.url.test --url=https://oldurl.com
    

    To see what urls are mapped to the sites:

    wp site list --network
    

    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/

  4. 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