InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Wordpress How-Tos

  • angularjs in wordpress
  • Convert wordpress page to posts and vice versa
  • Find all user created wordpress custom field keys
  • How to automate wordpress sandbox setup on Linux
  • How to capture php code or included file output in a variable
  • How to check the performance of a plugin using mysql query log
  • How to migrate wordpress from root to sub directory
  • How to upgrade wordpress manually
  • How to view wordpress current version
  • Setup xdebug for remote wordpress debugging
  • Wordpress - disable theme and plugin editing
  • Wordpress - get wpdb class method names
  • Wordpress multisite network vs normal installation
  • display method names from php WP_Query object
  • find if a wordpress page is leaf page
 
  • Home
  • > Tutorials
  • > Wordpress
  • > Wordpress How To

How to migrate wordpress from root to sub directory

By admin | Last updated on Mar 20, 2016

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.

Suggested posts:

  1. prevent wordpress xmlrpc.php attack
  2. Csslint – quick command-line tutorial and how to use it for pre-deployment sanity check
  3. How to list git branches
  4. Online tools to create comic strips
  5. SVG – beginner tutorial
  6. How to setup ssl (https) for your site on Ubuntu Linux
  7. How to automate wordpress sandbox setup on Linux
  8. Find which process is listening on a port on Linux
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Linux, Tutorials, Wordpress, Wordpress How To
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress