Steps to Install or Move WordPress Site to a Subdirectory

Steps to Install or Move WordPress Site to a Subdirectory 800x304 - Steps to Install or Move WordPress Site to a Subdirectory

Many small business owners choose WordPress, to power their website’s because of its flexibility. WordPress is relatively straightforward and easy to install however sometime many users, as most beginners do, end up installing WordPress in a subdirectory instead of the root directory (e.g. http://www.exampledomain.com/subdirectory/ instead of http://www.exampledomain.com). So if you accidentally installed WordPress in a subdirectory (or maybe want to move WordPress to a subdirectory) but don’t want to impede usability and user experience of your site (i.e. visiting your website with “http://exampledomain.com”), then you’re in the right place.

In this article, I will cover two scenarios: when WordPress site is installed in its own directory and when moving an existing WordPress install from the root directory to a subdirectory. For both scenarios, I will show “how to get rid of /subdirectory/ from your site’s URL” without affecting the user experience. Your visitors will not even know that files are stored in a subdirectory.

Note: Make sure you can access your hosting using any FTP client or CPanel (contact the website owner for the details). I have tested the following example on an Apache web server, as recommended at WordPress.org. Check if mod_rewrite module is enabled, and you should have some knowledge on .htaccess and file permissions.

When WordPress Installed in its Own Directory

Step 1: Fresh WordPress install in a subdirectory

Let’s say you’ve downloaded the latest stable release of WordPress and install it in a subfolder named “wordpress”. So the site is accessible using this URL (http://yourdomain.com/wordpress). Now continue with the following steps.

Step 2: Creating The .htaccess File

Login to the backend of your new WordPress site and navigate to Settings > Permalinks screen and set a proper permalink structure. Once you click “Save Changes” button, a .htaccess file will be generated automatically, which used for hardening site security, URL redirection, URL shortening, etc. Sometimes this .htaccess file doesn’t create or updated automatically. Then, you should check if WordPress has the write permission. If not setting the file permissions to 644 is recommended. If the issue persists, you should contact hosting provider to confirm the mod_rewrite module is enabled.

Step 3: Changing The Site Address

Now, navigate to Settings > General in the backend and look for the WordPress Address (URL) and Site Address (URL) fields. By default, these two fields come with the same value. WordPress Address is the address of the folder where you’ve installed WordPress, and the Site Address is your website’s URL.

Note: Both the address field must always include http:// and never end with the final slash.

 

WordPress General Settings - Steps to Install or Move WordPress Site to a Subdirectory

 

Save the settings you just edited, but don’t refresh your website until the following step 4 is completed.

Step 4: Finally, Dealing with index.php and .htaccess

Now, login to your hosting by using cPanel or a FTP client like FileZilla (FREE). Locate the index.php file and .htaccess file in the subdirectory (/wordpress in this example), and then copy and paste them to the domain root directory (these files should always be together in the directory).

Pay attention: We are just copying and pasting those files, not moving them from the WordPress directory.

WordPress index.php and htaccess - Steps to Install or Move WordPress Site to a Subdirectory
After copying the files, Open the index.php in the root directory of your domain in a plain text editor and replace the following line of code:

require( dirname( __FILE__ ) . '/wp-blog-header.php' );

Find and Replace the above line with the following one. Note that “wordpress” is the name of your own subdirectory.

require( dirname( __FILE__ ) . '/wordpress/wp-blog-header.php' );

Finally, save the file, that’s all you need to do. Now type the Site Address (“http://yourdomain.com”) in a browser and access your website.

 

When moving an existing WordPress install from the root directory to a subdirectory

if you’ve come this far then you might decide to clean your root directory by moving your existing WordPress site from the domain root to a subfolder/subdirectory. So let’s continue with the following steps.

Step 1: A complete backup of your WordPress site

The most important step, make sure you’ve taken a complete backup of your site because the next steps involve updating database tables, it’s better to be safe than sorry.

Step 2: Create a subdirectory and change the Address

Since we will move WordPress to a subdirectory, the first task in this step is to create a subdirectory in the root directory. You can use cPanel or a FTP client. Subdirectory name can be anything you want, and for this example, I used “/wordpress”( directory in which WordPress files will be stored, after moving from root directory).

After the subdirectory is created,  login to the backend of your site and navigate to Settings > General and change the URL values. Set WordPress Address to the subdirectory (/wordpress) URL and Site Address remains unchanged.

moving an existing WordPress install from the root directory to a subdirectory - Steps to Install or Move WordPress Site to a Subdirectory

Just like before, don’t refresh your website until the following steps are completed.

Step 3: Moving files to the subdirectory and modify index.php

Next, move the all WordPress install files from the root directory to the “/wordpress” folder, make sure to keep a copy of the index.php and .htaccess files in the root. Then from the root directory open the index.php file and replace the following line:

require( dirname( __FILE__ ) . '/wp-blog-header.php' );

Find and Replace the above line with the following one. Note that “wordpress” is the name of the subdirectory.

require( dirname( __FILE__ ) . '/wordpress/wp-blog-header.php' );

Click Save Button. With these changes in place, WordPress will look for wp-blog-header.php in its new location.

Step 4: Finally Update the Permalink Structure

Go to “http://yourdomain.com/wordpress/wp-admin” (“wordpress” refers to the name of the subdirectory) and once logged in, navigate to Settings > Permalinks to set up the permalink structure. During this process, the .htaccess file will be updated automatically.

Note: Even if you don’t make any changes, you should save the permalink structure once more.

Lastly, review the internal links in your menus and post content. To change menu links navigate to Appearance > Menus page. but changing links in existing posts are little tricky, it requires running queries from database, but the easy way is to use a plugin to do the same.

Using the Search & Replace plugin, you can comfortably search and replace strings in WordPress. Just go to Tools > Search & Replace in the backend of your site once you’re installed and activated the plugin.

WordPress Search Replace - Steps to Install or Move WordPress Site to a Subdirectory

About This Author

My name is Parameshwar Roy (P. Roy), and I am a web developer. This is my personal blog to record my own thoughts. Though I am not a natural writer, I love to share my experiences. Hope my experiences will be useful to you…read more about me

1 Comment

You can post comments in this post.


  • Your means of describing everything in this paragraph is truly awesome, all can simply know it, Thanks a lot. I save lot of time and everything work just perfect. Great!

    Greg 10 months ago Reply


Leave A Reply