Migrating wordpress from root to subdirectory is not a difficult task but should be done carefully. Here are the steps which can be followed.
http://yoursite.com/ | Old wordpress site |
http://yoursite.com/sub1/ | New wordpress site |
blogdb_old | old mysql database |
blogdb_sub1 | new mysql database |
Steps
- 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.
- Remove wp-config.php from sub1 and create empty mysql database blogdb_sub1 or whatever you want to name it.
- Run the installation process by opening htttp://yoursite.com/sub1/ in browser.
- 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.
- 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 andmysql
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) - 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.
- Now download wordpress search replace utility from interconnetit site and place the script in sub1 directory.
- 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.
- Remove script searchreplacedb2.php for directory sub1/ once all the search replace is done. Otherwise someone may accidentally mess up the database.
- If you have any links which needs to have /sub1/ in url, edit those.
- Copy your .htaccess from old location to sub1 directory and edit it.
Old .htaccess contentRewriteEngine 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
- 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.
- 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.