Install wordpress in subdirectory

New Page

Giving WordPress its own directory

Themes

  • Move a root installation to its own directory
  • Method I (since URL cambio)
  • Method II (with URL)
    • change process
    • modification de .htaccess
  • Move specific WordPress folders
  • See as well

Many people want WordPress to feed the root of their website (for example, http://example.com) but they don’t want all the WordPress files cluttering up their root directory. WordPress permet d’installer dans un sous-répertoire, but your website is served from the root of the website.

From version 3.5, users of sites several times can use all the functions listed below. If you are running a version of WordPress older than 3.5, update to install a multisite WordPress installation in a subdirectory.

Note to theme/plugin developers: this will not separate your code from WordPress. Themes and plugins will still reside in the wp-content folder.

Move a root installation to its own directory

Let’s say j’ai installed wordpress on example.com. You now have two different methods to move the wordpress installations to the subdirectory:

1) No SITE-URL change (example.com remains)

2) With change in SITE-URL (redirection vers example.com/subdirectory)

Arrival ↑

Method I (since URL cambio)

1) After I installed wordpress on the root tap, move EVERYTHING from the root tap to the subdirectory.

2) Create a .htaccess file in the root folder and put this content inside (just change example.com and my_subdir):

<IfModule mod_rewrite.c> Rewrite engine in RewriteCond %{HTTP_HOST} ^(www.)?example.com$ RewriteCond %{REQUEST_URI} !^/my_subdir/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !- d Rewrite rule ^(.*)$ /mi_subdirección/$1 RewriteCond %{HTTP_HOST} ^(www.)?example.com$ Rewrite rule ^(/)?$ my_subdir/index.php [L] </IfModule>

That’s all🙂

Arrival ↑

Method II (with URL)

Arrival ↑

change process

(ps If you’ve already installed WP in the subdirectory, some steps may have already been done automatically.)

  1. Create the new location to store the main WordPress files (we’ll use /wordpress in our examples). (Sous Linux, use mkdir wordpress from your www directory. You’ll probably want to use chown apache:apache in the wordpress directory you created).
  2. Go to the General Screen.
  3. En la dirección de WordPress (URL): establezca la dirección de sus principales archivos principales de WordPress. Ejemplo:http://example.com/wordpress
  4. En Dirección del sitio (URL): establezca la URL del directorio raíz. Ejemplo:http://example.com
  5. Click Save Changes. (Do not worry about the errors that happen now! Continue reading)
  6. Now move your WordPress core files (from root directory) to the subdirectory.
  7. Copy (NOT MOVE!) the index.php and .htaccess files from the WordPress directory into the root directory of your site (Blog address). The .htaccess file is invisible, so you may have to set your FTP client to show hidden files. If you are not using pretty permalinks, then you may not have a .htaccess file. If you are running WordPress on a Windows (IIS) server and are using pretty permalinks, you’ll have a web.config rather than a .htaccess file in your WordPress directory. For the index.php file the instructions remain the same, copy (don’t move) the index.php file to your root directory. The web.config file, must be treated differently than the .htaccess file so you must MOVE (DON’T COPY) the web.config file to your root directory.
  8. Open your root directory’s index.php file in a text editor
  9. Change the following and save the file. Change the line that says:require dirname( __FILE__ ) . ‘/wp-blog-header.php’;to the following, using your directory name for the WordPress core files: require dirname( __FILE__ ) . ‘/wordpress/wp-blog-header.php’;
  10. Login to the new location. It might now be http://example.com/wordpress/wp-admin/
  11. If you have set up Permalinks, go to the Permalinks Screen and update your Permalink structure. WordPress will automatically update your .htaccess file if it has the appropriate file permissions. If WordPress can’t write to your .htaccess file, it will display the new rewrite rules to you, which you should manually copy into your .htaccess file (in the same directory as the main index.php file.)

Arriba ↑

modificación de .htaccess

In some cases, some people like to install separate versions in a subdirectory (like /2010, /2011, /latest and etc.), and want this website (default) to use the latest version, then install WordPress in a subdirectory, like /my_subdir and in your root folder’s .htaccess file, add the following (just change the words as needed):

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

Now, when users access your root domain (example.com), they will automatically be redirected to the subdirectory you specified.

Note: This code is from site post 5 here: How to redirect your domain to a subfolder using .htaccess.

Arrival ↑

Move specific WordPress folders

The following links explain how to edit specific directories in WordPress:

  • Move the wp-content folder
  • Move plugin folder
  • Move themes folder
  • Moving the download folder

Arrival ↑

See as well

  1. Using Caddy to Give WordPress Its Own Directory
    • Migration de WP

      • mdb-database-migrationsDatabase migrations
      • mdb-mediafilesMedia Library Migrations
      • mdb-themepluginfilesMigrations of theme archives and add-ons
      • mdb-multisiteMultisite tools
      • mdb-cliCLI

      Features

    • Prix
    • Documents
    • Testimonials
    • Blog
    • What’s new ?
  • Purchase

The Ultimate Guide to Installing WordPress in a Subdirectory

# Posted on January 19, 2022

920dd5ed3b01d7185cc421e15e75ef07By Iain Poulson, Product Manager

install-wp-subdirectory-featured-img-1540x869

Dans cet article, nous examinons les avantages et les limites de l’installation de WordPress dans un sous-répertoire, comment installer et gérer les installations de sous-répertoires, et comment déplacer le noyau de WordPress depuis et vers un sous-répertoire pour un site existant.

One of the common criticisms PHP developers level at WordPress is that it installs all its core files directly in the web root directory. Modern PHP frameworks like Laravel install their required files as separate dependencies in a vendor directory, each residing in its own specific subdirectory. Not only that, but unlike WordPress, Laravel keeps almost all the PHP files out of a publicly accessible directory, which is very nice for security. This makes for a cleaner, more secure folder structure in the application’s web root.

These modern PHP application directory structures make a default WordPress install look decidedly unprofessional. While it’s not possible to move all the PHP files out of the public web root in a WordPress install, you can customize your installation to mimic this dependency-based development concept by installing WordPress in a subdirectory.

#Table of ContentsLink copied

  1. The Benefits of Installing WordPress Core in a Subdirectory
  2. How to Tell if WordPress Core is Installed in a Subdirectory
  3. How to Install WordPress Core in a Subdirectory
    1. The Official WordPress Documented Methods
    2. Manually Configure a Subdirectory Install
    3. Using WP-CLI to Install WordPress in a Subdirectory
    4. Using Composer to Install WordPress in a Subdirectory
  4. Considering the WordPress File and Folder Structure
    1. Moving the WordPress Config File
    2. Moving the WordPress Content Folder
  5. Converting a Root Install to a Subdirectory Install
    1. Prepare Your Site
    2. Update the Database
    3. Moving WordPress Files Into the Subdirectory
  6. Converting a Subdirectory Install to a Root Install
    1. Prepare Your Site
    2. Update the WordPress Database
    3. Moving WordPress Out of the Subdirectory
  7. Wrapping Up

#The Benefits of Installing WordPress Core in a SubdirectoryLink copied

In most web hosting environments, your account is provisioned under a /home/username folder where username is your account username. This folder is sometimes called the top-level or root of your account, and the actual path varies from web host to web host. You will typically have a public or public_html folder configured to serve the website files. This is generally known as the web root directory, and it’s where WordPress is usually installed.

The direct benefit of moving WordPress core files into a subdirectory is a cleaner and more professional-looking directory structure in your web root. Some folks think that switching to a subdirectory install improves security through obscurity. This is only really applicable if you move your wp-config.php file outside of the publicly accessible web root directory, whether you use a subdirectory install or not.

However, in the modern world of automated scanning bots, moving your WordPress core files to a subdirectory might help decrease the chances of malicious bots detecting vulnerabilities when scanning your site. For example, in a typical WordPress install, all your core files are in the public web root, which is accessible from your apex domain (e.g., https://hellfish.media). So all a scanner needs to do is scan your domain name, check for a common WordPress file like https://hellfish.media/wp-blog-header.php, and it will know you have a WordPress install. If you move the core files to a subdirectory, it makes it harder (but not impossible) for attackers to identify that you’re running WordPress.

A cleaner directory structure and an increase in security are sound reasons to move an install into a subdirectory, but there are some things you’ll need to know to accomplish this. You won’t be able to just move the WordPress install to a subdirectory and expect everything to work.

#How to Tell if WordPress Core is Installed in a SubdirectoryLink copied

With a WordPress install you’ve completed yourself, you’ll likely know if it’s installed in the web root (i.e., a root install) or a subdirectory. However, you might be in a situation where you inherit a site, or use a hosting provider’s one-click installer, or it’s been a really long time since the initial setup.

To determine if your WordPress install is in the web root or a subdirectory, head to your WordPress dashboard and navigate to the Settings > General screen. There you will see the “WordPress Address (URL)” setting and “Site Address (URL)” settings:

Standard WordPress installation configuration.

If the values match, you have a root WordPress install. If the “WordPress Address (URL)” has an additional path compared to the Site Address (URL), it’s in a subdirectory. You can also tell by looking at the URL when you log into your site. If WordPress was installed in a subdirectory, then the wp-admin page will be shown under that subdirectory (such as hellfish.media/wp/wp-admin/).

#How to Install WordPress Core in a SubdirectoryLink copied

There are a few different ways you can install the WordPress core files in a subdirectory.

#The Official WordPress Documented MethodsLink copied

The methods to move WordPress into a subdirectory are detailed in the WordPress.org support documentation. These options are available to you after you’ve already installed WordPress.

The two options are:

  1. Moving a WordPress install to its own directory, without changing the site URL
  2. Moving a WordPress install to its own directory, with a change to the site URL

Neither of these options really help with the increase in security we discussed earlier. The first option moves the site files to a subdirectory and uses rewrite rules in a custom .htaccess file to rewrite requests to the subdirectory. Any bots scanning your URL will still be able to determine that it’s using WordPress.

The second option is even less related to security, and more useful if you have a custom site setup. This is often used where you want to use WordPress to power a blog at a subdirectory of your URL (e.g., https://hellfish.media/blog) and you’ll use something else to power your top-level URL.

That being said, both methods move your WordPress install to a subdirectory, so your web root at least looks tidier.

What would be nice is that first option, but without any URL rewriting for better security.

#Manually Configure a Subdirectory InstallLink copied

If you recall the earlier description of how the files in a WordPress installation “require” each other, you might have also noticed that in the wp-blog-header.php file, the ABSPATH constant is defined. This constant points to the location where the WordPress core files are installed, no matter where that might be on the file system.

This means that with a little bit of knowledge and some tinkering, we can move all the WordPress core files into a subdirectory, and make a few small changes to let WordPress know where everything is. The process looks like this:

  1. Follow the default WordPress install process.
  2. Once WordPress is installed, create a subdirectory and move all the files from the web root to that subdirectory. To help with “hiding” the core files, you should give the subdirectory an uncommon name, but for our example, we’ll create a subdirectory called wp, and move all the files to the wp subdirectory.
  3. Then, copy (not move) the index.php file from the subdirectory back into the web root directory.
  4. In that newly copied file, update the line that requires the wp-blog-header.php file, to point to the new subdirectory, and save that file: require __DIR__ . ‘/wp/wp-blog-header.php’;
  5. The last step is to update the siteurl value in your WordPress options table— using something like the SQL Buddy plugin or phpMyAdmin—to point to the new subdirectory install, for example, https://hellfish.media/wp.

You’ll note we said “copy (not move)” in step 3. This is important because WordPress needs an index.php file in the install directory (in this case the subdirectory), or it will cause PHP warnings to display on your site or fill up your debug.log file.

Once you’ve completed these steps and all your core WordPress files are in a subdirectory, you will still be able to browse to the front end of your site at your top-level domain (e.g., https://hellfish.media/), but to access your Dashboard you’ll need to browse to wp-admin in the subdirectory (e.g., https://hellfish.media/wp/wp-admin). You might be concerned about losing the changes you’ve made to the copy of the index.php file the next time WordPress is updated. However, WordPress will only update core files in the installation directory, in this case, the subdirectory. So it’s perfectly acceptable to edit the index.php file you copied to the web root directory. Your web root is a little tidier, and it’s a bit harder to determine you’re running WordPress.

While this gives you a small increase in security, we’ll dive into other changes later on to further improve security.

#Using WP-CLI to Install WordPress in a SubdirectoryLink copied

You’re probably going to want to automate the above process if you are managing your own WordPress installs or you have your own VPS servers. Fortunately, this is possible with a little Bash scripting and WP-CLI. The following script leverages WP-CLI to create a new WordPress site installation with the core files in a specific subdirectory, essentially reproducing everything we did above. It also assumes you don’t already have the database created, and creates it for you. The process will still work if you have an existing database, but will display a MySQL error message that the database already exists.

#!/bin/bash # Installation: ## Download the script to your home directory # Make sure it has execute permissions (`chmod +x wp-install-core-sub-dir.sh`). # Install the script in one of the folders in your PATH. (`mv wp-install-core-sub-dir.sh /usr/local/bin/wp-install-core-sub-dir`) #Usage: # $ mkdir mysite # $ cd mysite # $ wp-install-core-sub-dir {sub-directory} {db_name} {db_user} {db_pass} {site_url} “{site_title}” {admin_user} {admin_pass} {admin_email} CORE_DIR=${1-‘wp’} DB_NAME=${2-‘wordpress’} DB_USER=${3-‘root’} DB_PASS=${4-‘password’} SITE_URL=${5-‘https://wordpress.test’} SITE_TITLE=${6-‘WordPress Site in a Subdirectory’} SITE_USER=${7-‘admin’} SITE_PASS=${8-‘password’} SITE_EMAIL=${9-‘[email protected]’} # create the dir for the core files mkdir $CORE_DIR cd $CORE_DIR # download WordPress files wp core download # create the wp-config.php file wp config create –dbname=$DB_NAME –dbuser=$DB_USER –dbpass=$DB_PASS # create the database wp db create # install WordPress (less than 5 mins) wp core install –url=$SITE_URL –title=”$SITE_TITLE” –admin_user=$SITE_USER –admin_password=$SITE_PASS –admin_email=$SITE_EMAIL # Copy (not move) index.php file to root cd ../ cp “$CORE_DIR/index.php” ./index.php # Edit index.php to point to correct path of wp-blog-header.php perl -p -i -e “s/\/wp-blog-header/\/$CORE_DIR\/wp-blog-header/g” index.php # Update the siteurl in the database with sub directory path cd $CORE_DIR wp option update siteurl $(wp option get siteurl)/$CORE_DIR # Uncomment the below line if you want the config in root #cp “$CORE_DIR/wp-config.php” ./wp-config.php echo ‘Install finished!’

You can also view this script on Github.

#Using Composer to Install WordPress in a SubdirectoryLink copied

A popular way to manage WordPress projects is to use Git for source control, and Composer to install WordPress core, the theme, and any plugins as dependencies in the composer.json file of your project.

When you’re ready to deploy your site, you trigger an automated deployment process, which runs composer install to install all your required dependencies.

If you use the johnpbloch/wordpress package, it will install the wordpress-core package in a subdirectory called wordpress by default. You can change this by supplying a custom wordpress-install-dir directory in the extra section of your composer.json. You can then make use of the Composer post-install-cmd event to run the necessary subdirectory changes you need.

{ “name”: “polevaultweb/wp-composer-core-sub-dir”, “description”: “Installing WordPress in a subdirectory with Composer”, “require”: { “johnpbloch/wordpress”: “^5.8” }, “extra”: { “wordpress-install-dir”: “wp” }, “scripts”: { “post-install-cmd”: [ “cp wp/index.php ./index.php”, “sed -i \”s/\\/wp-blog-header/\\/wp\\/wp-blog-header/g\” index.php” ] } }

As you can see, this replicates the subdirectory process we’ve already outlined earlier. Once the composer install has been completed, WordPress will be installed in the wp subdirectory, the index.php file will be copied to the web root, and edited to update the path to wp-blog-header.php

It is worth noting that the post-install-cmd event only fires after the install command has been executed with a lock file present. It won’t trigger the subdirectory the first time you run composer install locally, as the lock file is only generated after the first time composer install is run.

Unlike the Bash script above, this does not create the wp-config.php file or run the WordPress installation to create the database tables. You can do these manually in your browser, using WP-CLI commands in the post-install-cmd event, or using post deploy scripts if your hosting provider supports them.

#Considering the WordPress File and Folder StructureLink copied

As mentioned earlier, moving the core files to a subdirectory allows for a small security improvement, but it’s still possible to determine if your site is running WordPress. This might be a good time to think about any other changes we could make to improve our directory structure and increase security.

Our recommendation is to configure your top-level or root directory structure to look something like this:

/home/username/ conf/ wp-config.php public/ content/ wp/ index.php wp-config.php

We’ve already shown you how to move the WordPress core files into a subdirectory (in this case wp) so let’s see what else we can change, and why.

#Moving the WordPress Config FileLink copied

The wp-config.php file contains your database credentials, and often any other important API or third-party access keys, but did you know that it’s possible to move this file to a different location from the rest of your WordPress core files?

Wow.

Moving this file to somewhere other than the web root or the subdirectory install is generally considered a much safer choice. The reason for this is that in some cases, problems with the web server could cause it to stop processing PHP files correctly. Common causes for this include a lack of available memory, or a web server misconfiguration. Whatever the cause, if this happens, visitors to the site might be able to view the contents of PHP files including the database credentials in your wp-config.php file. In fact, many would argue that it should be placed even higher up in the directory tree than the root directory.

To achieve this, you would move the wp-config.php file to a different location on the server (in our case we’ll move it to the conf directory just above the public web root directory) and either comment out or delete all the code after the following line:

/* That’s all, stop editing! Happy publishing. */

We then create a new wp-config.php file in the public web root, with the following code:

<?php require_once __DIR__ . ‘/../conf/wp-config.php’; /** Sets up WordPress vars and included files. */ require_once ABSPATH . ‘wp-settings.php’;

WordPress will automatically look for the wp-config.php in the parent folder to where the core files are. By creating our custom wp-config.php file in the web root, we keep the wp folder that contains the core files clean of any custom files. The “true” wp-config.php file is moved to a location that’s not publicly accessible, but where WordPress can still require it, and your database (and other) credentials are safer.

#Moving the WordPress Content FolderLink copied

With our WordPress core files in a subdirectory, we could consider moving the wp-content directory too. Themes, plugins, and file uploads should (in most cases) be separate from the core files. This is especially useful if you use version control software like Git to manage your project source code.

WordPress allows us to accomplish this, by setting some WordPress-specific constants, in the wp-config.php file in our wp subdirectory.

define( ‘WP_CONTENT_DIR’, dirname( dirname(__FILE__) ) . ‘/content’ ); define( ‘WP_CONTENT_URL’, ‘https://’ . $_SERVER[‘HTTP_HOST’] . ‘/content’ );

This configuration tells WordPress to expect to find the content directory in a directory called content in the web root. It’s inspired by Mark Jaquith’s WordPress skeleton, with minor tweaks to match our directory structure. As Mark points out in the GitHub readme, it’s cleaner and the content directory is no longer in the scope of the wp directory.

If you make this change on an already existing site, you will need to move the contents of your wp-content directory (plugins, themes, uploads, etc.) to the new content directory. You will also need to run a Find & Replace on all your data. The goal is to update URLs for your media and asset files to point them to the new content URL, as well as ensuring any saved file paths are now pointing to the new content path.

If any external sites are linking to any of your media or assets, you’ll want to set up a path redirect, so that those links don’t throw 404 errors. This can be achieved by setting a path redirect using regex with a matching string of ^/wp-content/(.*) and setting the redirecting string to /content/$1. We recommend setting up this redirect at the web server level for best performance. If using SpinupWP, this can be done via the Path Redirects screen:

Setting up a route redirect to SpinupWP.

If you’re unable to set up a redirect at the web server level, the Redirection plugin is the next best thing.

#Converting a Root Install to a Subdirectory InstallLink copied

Installing the WordPress core files when starting a new site is straightforward enough, but converting an existing root install to a subdirectory install requires some care. It’s not simply a case of creating a subdirectory in your web root and moving all the WordPress files there. You also need to take into account any URLs or paths stored in the database that points to your root install directory. We cover the details of this in our WordPress Core Installed in a Subdirectory help doc, but let’s look at a simple example.

I’ve set up a local site, https://hellfishmedia.test, as a WordPress root install.

Installation from the root directory.

I’ve also created a very basic page to serve as the site home page, which includes an image in the page content, and a link to a Contact page.

Hellfish Media homepage.

#Step 1: Prepare Your SiteLink copied

If this is a live site, it’s a good idea to show a maintenance message while you make changes that could break the site. For this, we recommend the WP Maintenance Mode plugin. Install it, activate maintenance mode in the plugin settings, and your users will see a message letting them know about the brief period of downtime.

Maintenance message.

Front-end users will only see the maintenance page, but we can still access the WordPress dashboard through https://hellfishmedia.test/wp-admin/.

#Step 2: Update the DatabaseLink copied

For our example, we’ll be moving WordPress into a new /wp subdirectory, so you need to update the database to reflect that change – but only for URLs that need it.

You only need to worry about references to image files and assets contained in theme or plugin folders. All of those are located under /wp-content by default, or a custom content path, if you’ve configured one. As such, you can use the content path to target URLs and file paths you need to change. If you’ve configured a different location for your content as we described earlier, you might need to tweak this to match.

This is possible using any decent find and replace tool, including the WP-CLI search-replace command, Better Search Replace, or WP Migrate DB. For our purposes we’ll be using WP Migrate DB Pro‘s Find & Replace tool.

In our example site, we’ll want to find //hellfishmedia.test/wp-content and replace it with //hellfishmedia.test/wp/wp-content, and find /home/iain/development/websites/hellfishmedia/wp-content and replace it with /home/iain/development/websites/hellfishmedia/wp/wp-content to update URLs and file paths respectively. For the sake of the test, we’re also replacing the site title. Here’s what this will look like in WP Migrate DB Pro:

Custom search and replace with specified changes.

What’s great about the WP Migrate DB Pro Find & Replace tool is that you can preview the changes before you run them.

Custom preview of find and replace.

You can also inspect what updates the plugin will make on a per-table basis.

Once you run the find and replace, and take a look at any page content, you’ll see that while links to the Contact page don’t include the subdirectory, the embedded image does:

Show URL of refreshed content in code editor.

Your next step is to update the “WordPress Address (URL)” setting in your “General Settings” page to include the subdirectory. You can do this by navigating to Settings > General in your WordPress admin, and adding the subdirectory to the “WordPress Address (URL)” field. This will break the backend of your site until you complete the rest of this process, so make sure you don’t need to access it for a bit.

WordPress management update.

Once you save the change, your WordPress admin dashboard will show you a “Page not found” error and your maintenance page will lose all its styling. This happens because WordPress is still looking for all its admin and asset files under https://hellfishmedia.test/wp/, but they don’t exist.

#Step 3: Moving WordPress Files Into the SubdirectoryLink copied

While the maintenance page won’t look pretty at this point, at least it displays the maintenance message to your site users. Unfortunately, that’s going to break in the next step, as you need to move the files from the web root into the correct subdirectory. For our example, we’ll do the following:

  1. Create the wp subdirectory in the web root.
  2. Move everything from the web root into the wp directory.
  3. Copy (not move) the index.php file and the .htaccess file from /wp back into the web root directory.

You can do this via your local file manager, or via an FTP client if it’s a remote server.

Video Player

00:00

00:00

00:21

Use Up/Down Arrow keys to increase or decrease volume.

Alternatively, if you prefer the terminal, the commands would look like this:

cd ~/development/websites/hellfishmedia mkdir wp mv * .* wp/ cp wp/index.php . cp wp/.htaccess .

This will cause the maintenance page to display an error.

Warning: require(/home/iain/development/websites/hellfishmedia/wp-blog-header.php): Failed to open stream: No such file or directory in /home/iain/development/websites/hellfishmedia/index.php on line 17 Fatal error: Uncaught Error: Failed opening required ‘/home/iain/development/websites/hellfishmedia/wp-blog-header.php’ (include_path=’.:/usr/share/php’) in /home/iain/development/websites/hellfishmedia/index.php:17 Stack trace: #0 {main} thrown in /home/iain/development/websites/hellfishmedia/index.php on line 17

The last step is to update the require expression in the index.php file in the web root with the path to the new subdirectory, as we showed you right at the beginning of this guide. Edit the file in your code editor, and update the line to look like this:

require __DIR__ . ‘/wp/wp-blog-header.php’;

Alternatively, if you prefer to run a terminal command, you can use the same perl command that our WP-CLI script used earlier:

perl -p -i -e “s/\/wp-blog-header/\/wp\/wp-blog-header/g” index.php

Your root install should now be fully converted to a subdirectory install. You should take this opportunity to check all your content, make sure everything appears to be working, and that images display as you’d expect.

It’s also a good idea to log into your dashboard and navigate to Settings > Permalinks to save and flush your permalinks.

One last thing to note is that the example site we set up here doesn’t have a lot of plugins or a custom theme. These aspects could add additional complexities when converting between root and subdirectory installs. For this reason, we recommend making sure you’ve thoroughly tested the site after converting it from one install type to another.

#Converting a Subdirectory Install to a Root InstallLink copied

If you’ve completely converted to running all your WordPress sites as subdirectory installs, you might come across a situation where you need to work with a live client site that is a root install. If you use a tool like WP Migrate DB Pro, it’s not possible to migrate from a subdirectory install to a root install. This means you might need to first convert a local dev or staging site to a root install before migrating it to the live site. Converting a subdirectory install to a root install is almost always easier than the reverse because the URLs and paths are already using the subdirectory path you’re converting from. To do this, you’ll need to update both the database and the file structure, ideally with a minimal amount of downtime.

I’ve set up a local site, https://hellfishmedia.test, with WordPress installed in a subdirectory named wp.

Instalación del subdirectorio de WordPress.

I’ve also created a very basic page to serve as the site home page, which includes an image in the page content, and a link to a Contact page.

Página de inicio de Hellfish Media.

#Step 1: Prepare Your SiteLink copied

Converting the site is going to require some database changes but before you do this you’ll want to ensure that you can still access wp-admin. To achieve this, define the site URL and home URL values in the wp-config.php file. This will override the settings for siteurl and home in the wp_options table and will prevent you from losing access to the WordPress dashboard.

// The URL where the WordPress application files are accessible define( ‘WP_SITEURL’, ‘https://hellfishmedia.test/wp’ ); // URL where the front end is accessible define( ‘WP_HOME’, ‘https://hellfishmedia.test’ );

If the subdirectory site is live, it would be a good idea to show a maintenance message while you make changes that could break the site. For this, we recommend the WP Maintenance Mode plugin. Install it, activate maintenance mode in the plugin settings, and your users will see a message to let them know about a brief period of downtime, rather than a broken site.

Mensaje de mantenimiento.

However, we can still visit the dashboard using https://hellfishmedia.test/wp/wp-admin/.

The next step is to update the database.

#Step 2: Update the WordPress DatabaseLink copied

Now you can update the database to remove any references to the subdirectory. This is possible using any decent find and replace tool, including the WP-CLI search-replace command, Better Search Replace, or WP Migrate DB. For our purposes, we’ll be using the WP Migrate DB Pro Find & Replace tool.

To start, we search for the source site URL, including the subdirectory (//hellfishmedia.test/wp) and replace it with the URL excluding the subdirectory (//hellfishmedia.test). We also want to update any physical paths from /home/iain/development/websites/hellfishmedia/wp to /home/iain/development/websites/hellfishmedia. We’re also updating the site title for demonstration purposes:

Búsqueda y reemplazo personalizados con los cambios especificados.

As we mentioned earlier, WP Migrate DB Pro’s Find & Replace tool lets you preview changes before you run them.

Find & replace preview.

You can even inspect what updates the plugin will make on a per-table basis. Here I can confirm that the link to the image in the wp_posts table for my Home page will be updated, as well as the GUID for the image attachment. If I don’t want to update the GUID, I could alter this in the settings. In this case, it makes sense to change the GUID.

Find & replace differences.

You might not notice any changes after you run the find and replace. The front end will still show the maintenance page, while the back end will still work because of the hardcoded constants.

However, if you look at some post content, you’ll notice that the images are broken and internal links now lack the subdirectory. Here is the home page in the Block Editor:

Editing the home page.

To fix this, there’s one last step we have to do.

#Step 3: Moving WordPress Out of the SubdirectoryLink copied

Your last step is to manually move all the files in the subdirectory into the public web root directory of your site.

You drag and drop the files in your local file manager or use an FTP client if you’re working on a remote server. However, be sure to select the option to overwrite the index.php file:

Moving the files in the file manager.

Once you’ve copied over the files, you can delete the wp subdirectory.

You can also do this on the command line, but if your site is on a remote server you need to SSH into the server first. In this case, I’m working with my local hellfishmedia directory that we ran the find and replace on earlier.

cd ~/development/websites/hellfishmedia rm index.php cp -R wp/. . rm -rf wp

The last piece is to remove the WP_SITEURL and WP_HOME constants from your wp-config.php and then test your site.

It’s also a good idea to log into your WordPress dashboard and navigate to Settings > Permalinks. Here, click the Save Changes button to make sure you flush the permalinks and that your URLs still work.

#Wrapping UpLink copied

Hopefully, we’ve given you enough information to consider switching your root installs to subdirectory installs, as well as the tools to do so. If you’re looking for a modern, Composer-based option, you could consider using a similar structure as our SpinupWP spinupwp-composer-site package, which takes the idea of installing WordPress as a dependency to a whole new level.

Do you run your WordPress installs in a subdirectory? What workflows do you use to manage your WordPress directory structure? Do you think WP Migrate DB Pro should do more to make the migration process between the root and subdirectory more straightforward? Let us know your thoughts in the comments section below!

This entry was posted in Workflow, WP Migrate DB Pro and tagged subdirectory, Deployment, Development, Migration Tools, WordPress.

About the Author

920dd5ed3b01d7185cc421e15e75ef07

Iain Poulson Product Manager

Iain is a product manager based in the south of England. He also runs multiple WordPress products. He helps people buy and sell WordPress businesses and writes a monthly newsletter about WordPress trends.

@polevaultweb polevaultweb.com

ecluYCZdQ2Okuuvd4kIp_MDB-CTA-50

  • 799b5f52a93bf4dd82be35725a1f223b Sheiler says:

    May 15, 2018 at 8:04 am

    Your writing is really concise, clear, and helpful!

    Reply

  • d97fb1b65403545f71546591e7b54003 Treighton Mauldin says:

    May 15, 2018 at 8:06 am

    I dont see a good argument for keeping your local environment that different from production…if you made it so that people could migrate sites that did not follow the same structure i could see a lott of issues coming from it.

    Reply

  • 34e9470d89a4e9346326ea203856987d Mike Governale says:

    September 15, 2019 at 2:48 am

    Hi Jeff, thank you for your article, it’s very clear but I still have a message that my website has a “technical issue” and I can’t displaye it. I have my staging website in root directory : beta.wpogoniza.pl and I want to migrate all website in another server and to put all in a subdirectory: preprod.wallsstreet.eu/pl/. How you said, I replaced links from wp-content by the path of my subdirectory so: preprod.wallsstreet.eu/pl/ and all others links I leaved with the path of root directory: preprod.wallsstreet.eu. I tried to find app/public but I don’t have this path, I have public_html I tried to replaced also this path by subdirecotry and check both way but I have still message that my website has a technical issue. I don’t know if the problem comes from by DB or from my index.php or htaccess? I try everything but I can’t resolve it. Do you have some ideas what I should do?

    Reply

  • 416198b115d7d186a5d899f223fcf61f Maciej Palmowski says:

    January 23, 2022 at 2:47 pm

    Great article. You also could’ve mentioned about Bedrock, which is based on Composer + subfolder structure.

    Reply

How to Install WordPress in a Subdirectory (Step by Step)

Last updated on August 24th, 2022 by Editorial Staff | Reader DisclosureDisclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

Shares 295 Share Tweet Share Facebook Messenger WhatsApp Email

Do you want to install WordPress in a subdirectory?

Installing WordPress in a subdirectory allows you to run multiple WordPress instances under the same domain or even a subdomain name.

In this article, we’ll show you how to easily install WordPress in a subdirectory without affecting the parent domain name.

Installing WordPress in a subdirectory Installing WordPress in a subdirectory

Subdomain vs. Subdirectory? Which One is Better for SEO?

Normally, you would want to start a WordPress website on its own domain name (for example, wpbeginner.com).

However, sometimes you may want to create additional websites on the same domain name.

This can be done by either installing WordPress in a subdomain (http://newebsite.example.com) or as a subdirectory (http://example.com/newwebsite/).

One question that we get asked is which one is better for SEO?

Search engines treat subdomains differently from root domain names and assign them rankings as separate websites.

For instance, search engines consider WPBeginner and our WPBeginner Videos website as two separate websites.

On the other hand, sub-directories directly benefit from the domain authority of the root domain, thus ranking higher in most cases.

One way to create separate WordPress sites in both subdomains or subdirectories is by installing WordPress multisite network.

However, if you want to keep two websites managed separately, you can install different instances of WordPress.

You can also use WordPress site management tools to set up a single dashboard for managing your multiple WordPress installations.

Requirements for Installing WordPress in Subdirectory

There are no special requirements to install WordPress in a subdirectory. If you already have a WordPress website in the root domain name, then you are good to go.

However, if you have a static (non-WordPress website), then you may need to check with your hosting company to find out if they support WordPress.

If they don’t, then you’ll need to move your website to a different hosting company.

We recommend using Bluehost. They are offering a generous discount on hosting to WPBeginner users with a free domain name.

→ Click Here to Claim This Exclusive Bluehost Offer ←

If you are using a different hosting provider, then all top WordPress hosting companies make it very easy to install multiple WordPress websites using the same hosting account.

For instance, if you are using Bluehost, you can add a new WordPress website from your hosting dashboard.

Adding new websites in Bluehost Adding new websites in Bluehost

However, please keep in mind that most shared hosting accounts have limited server resources.

A sudden traffic spike on one of your websites will affect the performance and speed of all other websites on the same account.

If you are just starting out, then you can do that on shared hosting. Keep in mind that you’ll need to upgrade to a managed WordPress hosting as your business grows.

That being said, let’s take a look at how to easily install WordPress in a subdirectory.

Step 1. Create a Subdirectory under The Root Domain Name

First, you need to create a subdirectory or a folder under the main website. This is where you will install WordPress files.

Connect to your WordPress hosting account using a FTP client or File Manager in cPanel.

Once connected, go to the root folder of your website. Usually it is the /public_html/ folder.

If you already have WordPress installed in the root folder, you will see your WordPress files and folders.

Next, you need to right-click and select ‘Create new directory’ from the menu.

Create subdirectory Create subdirectory

You need to be careful when choosing the name for your subdirectory. This will be part of your new WordPress site’s URL and what your users will type in their browsers to reach this website.

For example, if you name this directory travel-guides, then your WordPress website’s address will be:

http://example.com/travel-guides/

Nuevo subdirectorio creado New subdirectory created

Step 2. Upload WordPress Files

Your newly created subdirectory is empty at the moment. Let’s change that by uploading WordPress files.

First, you need to visit the WordPress.org website and click on the download button.

Descargar WordPress Download WordPress

Your browser will now download the zip file containing the latest WordPress software to your computer.

After downloading the file, you need to select and extract it.

Mac users can double-click the file to extract it, and Windows users need to right-click and then select ‘Extract All’ option.

Extraer el archivo zip de WordPress Extract WordPress zip file

After extracting the zip file, you will see ‘wordpress’ folder containing all the WordPress files.

Archivos de WordPress extraídos Extracted WordPress files

Now let’s upload these files to your new subdirectory.

Connect to your website using an FTP client and go to the subdirectory you created in the first step.

In the local files panel of your FTP client, go to the ‘wordpress’ folder you just extracted.

Select all files inside the folder and then upload them to your new subdirectory.

Subir archivos de WordPress al subdirectorio Upload WordPress files to the subdirectory

Step 3. Create New Database

WordPress stores all your content in a database. You need to create a new database to use with your new WordPress site installed in a subdirectory.

First, log in to the cPanel dashboard of your WordPress hosting account. Click on ‘MySQL Databases’ under the databases section.

Creating a MySQL database Creating a MySQL database

Note: Your hosting dashboard may look different than the screenshot above. You simply need to locate the ‘Databases’ section.

On the next screen, enter a name for your new database and then click on the ‘Create Database’ button to continue.

Create a new database Create a new database

Your cPanel dashboard will now create the new MySQL database. Click on the Go Back button to return to the Databases page.

Next, you need to add a username and password for the database.

Simply scroll down to the ‘MySQL Users’ section and provide a new username and password. Click on ‘Create User’ button to continue.

Create database username and password Create a database user and password

Next, you need to give this newly created user privileges to work on the database you created earlier.

Scroll down to the ‘Add user to database’ section. Select your MySQL username and then select your newly created database.

Add user to database Add a user to the database

Click on the Add button to continue.

Cpanel will now grant the MySQL user full privileges on your newly created database.

Step 4. Install WordPress

Now that everything is in place, you can go ahead and install WordPress.

Simply visit the directory you created earlier in a web browser by typing the URL like this:

http://example.com/your-subdirectory-name/

This will bring up the WordPress installation wizard. First, you need to select the language for your WordPress website and click on the continue button.

Select idiom Choose language

A continuación, se le pedirá que proporcione el nombre de su base de datos de WordPress, el nombre de usuario de la base de datos, la contraseña y el host. Ingrese los detalles de la base de datos y haga clic en el botón enviar.

Proportion of database details Provide your database details

WordPress ahora se conectará a su base de datos y verá un mensaje de éxito como este:

Connected WordPress database Connected WordPress database

Haga clic en el botón ‘Ejecutar la instalación’ para continuar.

In the screen suite, you will be asked to provide a title for your website and choose an administrator username, password and email address.

WordPress website details WordPress site details

After entering your website details, click the ‘Run Setup’ button to continue.

WordPress will now configure your website and show you a success message:

WordPress installed with éxito in the subdirectory WordPress successfully installed in subdirectory

Ahora can continue and start session in your new WordPress website installed in the subdirectory.

Étape 5. Fix permalinks

If you ever have a separate WordPress installation in the root directory, then the .htaccess files in your subdirectory will cause a conflict. This will result in 404 errors on your website.

To resolve this issue, edit the .htaccess archives in the WordPress installation subdirectory. Replace the dentro code of su archivo .htaccess with the following code:

1

2

3

4

5

6

sept

8

9

dix

11

# START WordPress

<IfModule mod_rewrite.c>

Rewrite engine enabled

RewriteBase /your-subdirectory/

^index\.php$ rewrite rule – [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

Rewrite the rule. /your-subdirectory/index.php [L]

</IfModule>

  

# FIN WordPress

Don’t forget to replace /your-subdirectory/ with your own subdirectory name.

Hope this article helps you install WordPress in a subdirectory. Also it is possible that you will see the ultimate WordPress SEO guide for beginners.

If you like this article, submit to a new YouTube channel for WordPress video tutorials. Also puede encontrarnos on Twitter and Facebook .

Shares 295 Share Tweet Share Facebook Messenger WhatsApp Email

Google Google translator

Google Google Translate


Video Install wordpress in subdirectory

Related Posts

Free chat room code for my website

Contents1 How to set up a free chat room on the website.2 3 comentarios2.1 Trackbacks/Pingbacks2.2 Submit a Comment Cancel reply3 How to Create a Chat Room Website…

Background image full screen css

Contents1 Cómo – Full Page Image1.1 Example1.2 Example2 CSS background image tamaño tutorial: how to codify a complete page background image3 Perfect Full Page Background Image3.1 Méthode CSS géniale,…

WordPress leverage browser caching

Contents1 Aproveche el almacenamiento in hidden del navegador1.1 Will it works for my website?1.2 Where are plugin options1.3 Some JavaScript files still display under Leverage Browser Caching1.4…

WordPress post to facebook page

Contents1 How to Automatically Post to Facebook from WordPress1.1 Download Now: How to Launch a WordPress Website [Free Guide + Checklist]1.2 1. Create an IFTTT account.1.3 2….

Download images from wordpress media library

Contents1 How to export your WordPress media library1.1 Download maintenant : How to launch a WordPress website [Free Guide + Checklist]1.2 How to export your WordPress media…

WordPress single post template

Contents1 How to Create Custom Unique Post Templates in WordPress2 Post Template Files2.1 author.php2.2 Fecha.php3 Handbook navigation4 How to Create Custom Single Post Templates in WordPress5 Video…