How to migrate wordpress from root to sub directory

Migrating wordpress from root to subdirectory is not a difficult task but should be done carefully. Here are the steps which can be followed.

Terminology
http://yoursite.com/ Old wordpress site
http://yoursite.com/sub1/ New wordpress site
blogdb_old old mysql database
blogdb_sub1 new mysql database

Steps

  1. Copy all the wordpress folder to a new folder and move it to main wordpress directory as subdirectory. Lets call it sub1 for the purpose of this post.
  2. Remove wp-config.php from sub1 and create empty mysql database blogdb_sub1 or whatever you want to name it.
  3. Run the installation process by opening htttp://yoursite.com/sub1/ in browser.
  4. Create wp-config.php in sub1/ directory as asked by installation process. You don’t need to complete the full process. This will prevent creating tables in the db.
  5. Copy old database to the new database. You can take database export and import. In unix you can use command line utility mysqldump for export and mysql to import. If you are using some mysql GUI tool (e.g. Sequel Pro on mac), then look for mysql export options and export as sql file. Avoid export in wordpress admin ui.
    Unix commands for export and import:
    mysqldump -u root -p yourpassword -h localhost blogdb_old > blogdb_old.sql
    (assuming blogdb_old is old db)
    cat  blogdb_old.sql | mysql -u root -p yourpassword -h localhost blogdb_sub1
    (assuming new mysql database blogdb_dir1 exists and empty)
  6. Edit wp-options table in database blogdb_sub1 and change siteurl and home to http://yoursite.com/sub1. If this is not done, you won’t be able to open http://yoursite.com/sub1/wp-admin. You should use some mysql GUI tool to achieve this step.
  7. Now download wordpress search replace utility from interconnetit site and place the script in sub1 directory.
  8. Open the search replace script (http://yoursite.com/sub1/searchreplacedb2.php) and follow the instruction and replace http://yoursite.com with http://yoursite.com/sub1 in table wp_posts and wp_options. In simple case no other table have to be edited. You may want to double check if any table needs to be edited depending upon your content/plugins.
  9. Remove script searchreplacedb2.php for directory sub1/ once all the search replace is done. Otherwise someone may accidentally mess up the database.
  10. If you have any links which needs to have /sub1/ in url, edit those.
  11. Copy your .htaccess from old location to sub1 directory and edit it.
    Old .htaccess content

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    New .htaccess content

    RewriteEngine On
    RewriteBase /sub1/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /sub1/index.php [L]

    You may want to remove old .htaccess

  12. Assuming you have significant number of posts in the wordpress database and those are indexed by google/other search engines. It may be a good idea to have HTTP 301 redirects so that users are automatically redirected to new urls and Google also updates its index. Use this redirect logic
    RewriteCond %{REQUEST_URI} !^/sub1
    RewriteRule ^/(.*) http://yoursite.com/sub1/$1 [L,R=301]

    This is assuming you have no other subdirectory and root php/html page. Otherwise add RewriteCond above accordingly.

  13. In case you have submitted a sitemap to google webmaster tool, you may want to update the new sitemap path. Best is to remove old sitemap and add the new one.
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments