How to change your WordPress URL when installed in a subfolder

Oftentimes people install WordPress in a subdirectory of the main website. One-click installers like to do this. Say your domain is http://example.com, but WordPress lives in http://example.com/wordpress. By default this means that people will have to visit your website at the latter URL.

But what if you want your URL to be http://example.com, without that /wordpress at the end?

No problem I say – let me talk you through it. It may sound scary, but that’s just because we have to do some tweaks to files we don’t normally touch.


You have to do three things:

  • change database values and tell WordPress it shall present itself somewhere else
  • add a tweaked version of index.php to your root directory
  • add some statements to your .htaccess file

Have your favourite text editor and FTP client ready and let’s get started!

Change Database Values for site and home

Login to your site and head over to Settings – General. You should see two URL fields there which by default contain the same value:

Screen Shot 2013-10-12 at 11.37.54

The first URL is where WordPress physically lives on the server. The second URL is what’s displayed in a visitor’s browser. In our case, we want to change the second URL to http://example.com, like so:

Screen Shot 2013-10-12 at 11.38.16

Hit save and you’re done in the admin area. Note that doing so means you will lose access to your site for a moment! Don’t panic though, because we’re about to fix that next.

Tweaking index.php

The index.php file is a default file that browsers look for when you point them at a directory. If that file is there it is loaded automatically and will be executed. If it’s not, the browser will look for similar files, such as

  • start.htm / start.html
  • index.htm / index.html

In our example, when we navigate to http://example.com it is likely that there’s nothing there, or perhaps one of the files mentioned above from a default or previous installation. Make sure all those are deleted in the root, so that the one we’re about to create (index.php) is the only such file in the root directory. If you leave any of the other standard browser files, you can’t really guarantee which one is loaded – and we want to make sure index.php is the only one that’s there.

We need a copy of the index.php file that’s inside your WordPress installation (for us it’s http://example.com/index.php). Download it with your favourite FTP client, then edit it with your favourite ext editor. You’ll see something like this:

/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');

See that very last line? That’s the one that kicks off WordPress. It’s saying “load a file in this folder”. And that would work great if WordPress would live in this current folder. However, we want to tell it that it needs to load a file from the /wordpress folder.

Change that last line to point at the folder in which WordPress lives. Amend this to match your WordPress directory:

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

Now save the file and upload it to your root directory. Don’t overwrite the original file inside your /wordpress folder – it needs to stay there. We need to put the file so that it can be accessed at http://example.com/index.php

Tweaking .htaccess

If you’re not using Pretty Permalinks your site is probably operational already (http://example.com/?p=47 as opposed to http://example.com/my-latest-post).

The .htaccess file is a configuration file for your remote web server. It’s the service that returns websites when a browser requests it. We need to tell it that when the browser is asking it to serve up http://example.com/my-latest-post, that really it needs to serve up http://example.com/wordpress/my-latest-post.

This is done with something called a mod_rewrite rule. Don’t worry if this sounds weird, it’s not something you use everyday. It’s the same engine that makes it possible to have those nicely formatted Permalinks.

.htaccess is a hidden file, so make sure you tell your favourite FTP client that you want to see hidden files – otherwise you may not find it. Also, if you’re not using Permalinks you may not have a .htaccess file – in which case, just create an empty file.

Download your .htaccess file and add the following at the end:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ wordpress [L]

Replace “example.com” with your own domain, and “wordpress” with your actual WordPress subfolder. Save the file and upload it to the root directory of your site (leaving the one inside your WordPress folder untouched, just like we did with the index.php file).

Now head over to your personal http://example.com and see your site working fine, just like it was installed in the root folder. When you login, you’ll still have to do so via http://example.com/wordpress/wp-login.php as you did before.

My head hurts… can you do this for me?

No problem – book a half hour support slot here and I can do all this scary stuff for you.

Further Reading: http://codex.wordpress.org/Giving_WordPress_Its_Own_Directory





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.