How to serve WordPress on the main domain from a subdirectory

- by

If WordPress is installed in a subdirectory, the URL to the main page is something like domain.com/subdir. That might not be what you want, and there’s a way to change this behaviour. To make it happen we need to change two options that are not immediately obvious:

  1. in the WordPress admin section, head over to Settings – General and update the value for Site URL to domain.com (while leaving the WordPress URL at its current position)
  2. add a new index.php file that starts loading WordPress in the root directory (often overlooked and sparsely documented)

I’ll talk about both points, as well as some general troubleshooting that may prevent your installation from loading as desired.

New index.php in root directory

Let’s start with the less obvious second part of the puzzle first: the new index.php file. Here’s what it looks like:

<?php
define( 'WP_USE_THEMES', true );

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

It’s short and sweet. Aside from defining a constant, it will load the first file that starts the execution of WordPress in the subdirectory. Replace “subdirectory” part in the last line with your actual subdirectory.

Tweak the Site URL

Next we need to tell WordPress where the public facing front page should be served from. Head over to Settings – General and tweak the Site Address value. This is likely set to the same value as the WordPress Address (which includes the subdirectory). Remove the subdirectory only in the Site Address field and leave the WordPress Address field untouched. See example below.

When you hit save, the new value is saved in the database. If your new index.php file is not in place, the front page of your site may not be accessible. The admin backend is unaffected by this change.

There’s a link in this section that explains how to move WordPress into a subdirectory. It does not however tell you how to do it properly at the time of writing.

Save Permalinks again

The last step in the process has to do with making sure all Apache/NGINX permissions are updated. The easiest way to do this is to head over to Settings – Permalinks and by pressing the save button here. Don’t change the actual permalink structure, leave whatever is selected in place. Saving will make WordPress re-create the .htaccess file(s) in the correct place(s), updating rules as necessary.

If all goes well, WordPress is now served from the subdirectory, but it will appear to come from the main domain. Hurray!

Note that you must still use the subdirectory to access the backend.

  • Front Page is now domain.com
  • Admin Interface is still domain.com/subdirectory/wp-admin

Troubleshooting Tips

If your backend performs fine, but the front page still isn’t behaving as you had expected, outdated .htaccess files may be in place. These may re-direct the site incorrectly. Remove any you find in the root directory as well as the subdirectory, then re-save your permalinks.

If you believe you have made a mistake with the Site URL or WordPress URL tweak and cannot access the backend anymore, get access to your database via the MySQL command line or phpMyAdmin. Navigate to the Options table and adjust the values for siteurl (that’s the Site Address) and home (that’s the WordPress URL).

Good luck!



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.