Safely Move WordPress from a Subfolder to Root and Redirect Old Links to New Links

Posted on November 12, 2013 in WordPress Category with Hard Difficulty

Moving your WordPress from a subfolder to root is a great idea and practical too. Why? Because of a higher Page Rank. Usually websites that are located in domain root performs better on search engines.

In the following tutorial we will teach you how to safely move your WordPress to root and redirect every visitor from your old links to the new ones. Here are few examples of what you are going to do.

Move your WordPress from "/blog" folder (or from any folder you have your WordPress installed)...

... to your domain root.

And redirect old links       or

To new links       or

Pretty simple, but hard enough so let's get started. Remember to not rush things up, be careful to not delete something and follow the steps one by one. Also is important to do a full backup before doing this.

Step 1 - Take a Backup First

Before starting the process, be sure to backup your WordPress, root files and SQL databases. You can save everything to your computer very easy from cPanel. Check the images below.


Step 2 - Move Root Files to Another Folder

After backup, make sure to move your root files to another folder in order to not overwrite with your WordPress files.

If you are running a portfolio in your domain's root, simply move every file to another folder, doesn't matter if it's htaccess, styles, index files, images, and let space for WordPress migration. You can change paths later in these files.

If you are running another WordPress in root folder, you can follow the same steps on moving.

Step 3 - Edit the New Cloned SQL Database File

Now that you have the SQL file to your computer, you will need to create a new database with all the links updated.

Simply clone/copy the SQL file anywhere you want on your computer to let the original intact and open the cloned file with any editor that have the Find and Replace Function. We used Notepad++ because opens big files very fast.

So we are looking to find and replace the old links ( with the new ones (

In the example below we will use our domain. Simply click "Replace All" to replace all links.


In the SQL file there are still paths which are using your server paths. You'll need to replace that too.

Below is an example of this kind of paths. As a solution you can search by word "blog/" which is not so accurate or you can search in steps for example, replace a longer word "public_html/blog" with "public_html", replace and after you can search again for the word "blog/"

Every path which contains your folder name, should be deleted. See a sample path below.


After cleaning everything, you can upload your new SQL file to your server.

Step 4 - Create a New Database and Import the New SQL File using phpMyAdmin

Using MySQL Database Wizard from cPanel , you can create a new database very easy. While creating your database do not forget to check all privileges when requested.


Great! Your new database is up and running. Now let's upload the SQL file to the database using phpMyAdmin.


Step 5 - Copying WordPress Files to Root

Using cPanel's File Manager, do NOT move all the files because you will need the WordPress intact to redirect old links on the new links from root. Simply just select all files and copy them to root location.



Step 6 - Connecting to the New Database

After creating the new database you need to point the WordPress to this new  SQL Database. In the root folder where you moved your WordPress you will find a file called "wp-config.php". Here are the MySQL settings (username, password, database name etc.). Open the file with any text editor you like and update MySQL settings with the new names and passwords.

// ** MySQL settings ** //
define('DB_NAME', 'new_db_name'); // The name of the database
define('DB_USER', 'new_user_namet'); // Your MySQL username
define('DB_PASSWORD', 'new_user_pass'); // ...and password
define('DB_HOST', 'localhost'); // 99% chance you won't need to change this value
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');


Step 7 - Cleaning All Paths and Links from .htaccess

If you have W3 Total Cache Plugin or something similar for caching, these plugins are rewriting your .htaccess file with absolute path of your blog. Make sure to check the .htaccess file to identify these paths and delete the folder where your old WordPress points. If you don't change the .htaccess all posts from your new blog may redirect to your old blog.

In the example below the old WordPress was in the "blog" folder. Simply delete it together with the front slash ("blog/") and save the file.


Great! Now your new moved WordPress should work perfect if you changed all your links and paths correctly.

The last thing is to redirect the old blog posts to the ones from the root.

Step 8 - Redirecting Old Blog Posts to New Posts

Redirecting the old links to the new link can be done in 2 ways. Installing a plugin that does the job for you or by placing a JavaScript Function that grabs your link and redirect it to root domain.

For example you want to redirect this:       or

To this:       or

Installing Redirection Plugin for WordPress

Grab this plugin and install it to your old blog (/blog/wp-admin) .

After installing the plugin, you will find it in the Tools Tab. Write the following settings:

Source URL:          /blog        (old wordpress folder name)
Match:               URL Only
Action:              Pass-through
Regular expression:  Checked
Target URL:          /


Redirecting with JavaScript

There are also many methods to redirect links with JavaScript, but the simplest way is to get the actual URL and delete the folder name from it, so you can redirect to the new link. You can do this by using the replace() function.

Copy the code below right under the <head> tag in header.php file (should be in the theme folder you are using which is located in wp-content/themes).

<script type="text/javascript">
	var oldURL = location.href;
	var wordYouWantReplaced = "blog/";
	var wordToReplace = "";
	var newURL = oldURL.replace(wordYouWantReplaced, wordToReplace);
	window.location.href = newURL;

Step 9 - Testing Everything

After finishing your work, you can finally test everything. Please note that some Text Widgets may dissapear. If that happens, you still have the widgets from your old blog, so you can copy paste the text.

Related Articles
  1. David says:

    Mate, do you have any latest tutorials on this? what about plugins.. will I have to install them again on the root folder WP?

    Well written.

    Cheers David

    • Hello David,

      I will try to test a new method on how to move WordPress faster and write a new tutorial with all steps.

      Meanwhile, if you have all your files saved, you don’t need to install all plugins again, because you already moved them in your new folder.

      Anyway, what exactly are you trying to do?

      • David says:

        Hi Dan,

        I am trying to move to

        I am thinking to use js instead of plugin, so as per your article the new code will be:

        var oldURL = location.href;
        var wordYouWantReplaced = "";
        var wordToReplace = "";
        var newURL = oldURL.replace(wordYouWantReplaced, wordToReplace);
        window.location.href = newURL;


        Regards, David

        • Hello David,

          Right. The code you wrote should be correct. Have you tested it and is not working?

          • David says:

            Hi Dan,

            Thanks for asking. I am still in the process of copying the files in the root. i.e. Step 5.

            Will post here how things goes.. so others can also benefit.

            Hope its ok.

          • David says:

            Worked perfectly fine now. Thanks!

  2. Philipe says:

    dude!! thanks so much for your help ! it worked perfectly

Hint: Wrap your code syntax (html, css or others) between <pre> and </pre> tags. All comments are moderated.

2 × two =

Subscribe by E-mail

HTML-TUTS is built under the mighty WordPress platform.