Wordpress: Move Multisite from server to localhost

前端 未结 4 1206
情话喂你
情话喂你 2021-02-06 00:04

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 impo

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-06 00:42

    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.

提交回复
热议问题