Installing wordpress in a subdirectory

New Page

Cómo Install WordPress in a subdirectory (Paso a Paso)

Last Updated On Aug 24, 2022 By Editorial Team | Reader Disclosure Disclosure : Our content is endorsed by the reader. This means that if you click on some of our links, we may earn a commission. See how WPBeginner is funded, why it’s important, and how you can support us.

Shares 295 Share Tweet Share Facebook Messenger WhatsApp Email

¿Quieres install WordPress in a subdirectory?

Installing WordPress in a subdirectory allows multiple instances of WordPress to be ejected under the mismo dominio or incluso a number of subdominio.

In this article, we will show you how to easily install WordPress in your own directory without affecting the main domain number.

Install WordPress in a subdirectory Install WordPress in a subdirectory

Subdomain vs Subdirectory? Which is better for SEO?

Normally, the initiator of a WordPress website has its own domain number (eg, wpbeginner.com).

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

You can install WordPress in a subdomain (http://newwebsite.example.com) or in a subdirectory (http://example.com/newwebsite/).

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

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

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

On the other hand, subdirectories benefit directly from the domain authority of the root domain, so they rank higher in most cases.

A form of creating separate WordPress sites in subdomains or subdirectories is installed in WordPress Red Multisite.

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

You can also use the WordPress site administration options to set up a single panel for the administrator of multiple WordPress installations.

Requirements for Installing WordPress and Subdirectory

No special requests for installing WordPress in a subdirectory. If you are on a WordPress website in the number of dominio raíz, entonces está listo para comenzar.

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

If not, you will need to move your website to another hosting company.

Recommend the use of Bluehost. They are offering a generous discount on hosting for WPBeginner users with a free domain name.

→ Click here to grab this exclusive offer from Bluehost ←

Si está utilizando un prouveur d’alojamiento diferente, todas las principales empresas de alojamiento de WordPress facilite l’installation de varios sites web de WordPress con la misma cuenta de alojamiento.

Par exemple, si vous utilisez Bluehost, vous pouvez accéder à un nouveau site web de WordPress sur le tableau d’hébergement.

Aggregate new websites on 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 a subdirectory Create a 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/

New subdirectory created 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.

Download 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.

Extract the WordPress zip file Extract the WordPress zip file

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

Extracted WordPress Files 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.

Upload WordPress files to subdirectory Upload WordPress files to 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 a database user 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 a user to the 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.

Choose language Choose language

Next, you will be asked to provide your WordPress database name, database username, password, and host. Enter the database details and click on the submit button.

Provide your database details Provide your database details

WordPress will now connect to your database and you will see a success message like this:

Connected WordPress database Connected WordPress database

Click on ‘Run the install’ button to continue.

On the next screen, you will be asked to provide a title for your website and choose an admin username, password, and email address.

WordPress site details WordPress site details

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

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

WordPress installé avec succès dans le sous-répertoire WordPress installé avec succès dans le sous-répertoire

You can now go ahead and log in to your new WordPress website installed in the subdirectory.

Step 5. Fix Permalinks

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

To solve this, you need to edit the .htaccess file in your subdirectory WordPress install. Replace the code inside your .htaccess file with the following code:

1

2

3

4

5

6

7

8

9

10

11

# BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /your-subdirectory/

RewriteRule ^index\.php$ – [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /your-subdirectory/index.php [L]

</IfModule>

  

# END WordPress

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

We hope this article helped you install WordPress in a subdirectory. You may also want to see our ultimate step by step WordPress SEO guide for beginners.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Shares 295 Share Tweet Share Facebook Messenger WhatsApp Email

Giving WordPress Its Own Directory

Topics

  • Moving a Root install to its own directory
  • Method I (Without URL change)
  • Method II (With URL change)
    • Moving process
    • .htaccess modification
  • Moving Specific WordPress Folders
  • See also

Many people want WordPress to power their website’s root (e.g. http://example.com) but they don’t want all of the WordPress files cluttering up their root directory. WordPress allows you to install it into a subdirectory, but have your website served from the website root.

As of Version 3.5, Multisite users may use all of the functionality listed below. If you are running a version of WordPress older than 3.5, please update before installing a Multisite WordPress install on a subdirectory.

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

Moving a Root install to its own directory

Let’s say you’ve installed wordpress at example.com. Now you have two different methods to move wordpress installations into subdirectory:

1) Without change of SITE-URL (remains example.com)

2) With change in SITE-URL (it will redirect to example.com/subdirectory)

Top ↑

Method I (Without URL change)

1) After Installing the wordpress in root folder, move EVERYTHING from root folder into subdirectory.

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

<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} ^(www.)?example.com$ RewriteCond %{REQUEST_URI} !^/my_subdir/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /my_subdir/$1 RewriteCond %{HTTP_HOST} ^(www.)?example.com$ RewriteRule ^(/)?$ my_subdir/index.php [L] </IfModule>

That’s all🙂

Top ↑

Method II (with URL change)

Top ↑

Moving process

(ps If you have already installed WP in a subdirectory, some steps may already be done automatically).

  1. Créez le nouvel emplacement pour stocker les fichiers principaux de WordPress (nous utiliserons /wordpress dans nos exemples). (Sous Linux, utilisez mkdir wordpress depuis votre répertoire www. Vous voudrez probablement utiliser chown apache:apache sur le répertoire wordpress que vous avez créé.)
  2. Accédez à l’écran général.
  3. In WordPress address (URL): set the address of your main WordPress core files. Example: http://example.com/wordpress
  4. In Site address (URL): set root directory’s URL. Example: 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.)

Top ↑

.htaccess modification

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.

Top ↑

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

Top ↑

See also

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

      • mdb-database-migrationsDatabase migrations
      • mdb-mediafilesMedia library migrations
      • mdb-themepluginfilesTheme and Plugin File Migrations
      • mdb-multisiteMultisite tools
      • mdb-cliCLI

      Features

    • Pricing
    • Documents
    • Testimonials
    • Blog
    • What’s new?
  • Acheter

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:

Paramètres d'installation standard de WordPress.

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?

Ouah.

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:

Mise en place d'une redirection de chemin sur 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 du répertoire racine.

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.

Page d'accueil Hellfish Media.

#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.

Message d'entretien.

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:

Recherche et remplacement personnalisés avec les modifications spécifiées.

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

Aperçu personnalisé de recherche et de remplacement.

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:

Affichage des URL de contenu mises à jour dans l'éditeur de code.

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.

Mise à jour de l'adresse WordPress.

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.

Installation du sous-répertoire 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.

Page d'accueil 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.

Message d'entretien.

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:

Recherche et remplacement personnalisés avec les modifications spécifiées.

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

Rechercher et remplacer l'aperçu.

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.

Rechercher et remplacer les différences.

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:

Modification de la page d'accueil.

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

# Paso 3: Mover WordPress fuera del subdirectorioLink copied

Su último paso es mover manualmente todos los archivos del subdirectorio al directorio raíz web público de su sitio.

Arrastras y sueltas los archivos en tu administrador de archivos local o usas un cliente FTP si estás trabajando en un servidor remoto. Sin embargo, asegúrese de seleccionar la opción para sobrescribir el index.phparchivo:

Moving files in the file manager.

Una vez que haya copiado los archivos, puede eliminar el wpsubdirectorio.

También puede hacer esto en la línea de comando, pero si su sitio está en un servidor remoto, primero debe ingresar al servidor mediante SSH. En este caso, estoy trabajando con mi hellfishmediadirectorio local en el que ejecutamos la búsqueda y el reemplazo anteriormente.

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

La última pieza es eliminar las constantes WP_SITEURLy de su sitio y luego probarlo.WP_HOMEwp-config.php

También es una buena idea iniciar sesión en su panel de control de WordPress y navegar a Configuración > Enlaces permanentes . Aquí, haz clic en el botón Guardar cambios para asegurarte de eliminar los enlaces permanentes y que tus URL aún funcionen.

# TerminandoLink copied

Con suerte, le hemos brindado suficiente información para considerar cambiar sus instalaciones raíz a instalaciones de subdirectorio, así como las herramientas para hacerlo. Si está buscando una opción moderna basada en Composer, podría considerar usar una estructura similar a nuestro paquete SpinupWP spinupwp-composer-site , que lleva la idea de instalar WordPress como una dependencia a un nivel completamente nuevo.

¿Ejecutas tus instalaciones de WordPress en un subdirectorio? ¿Qué flujos de trabajo utiliza para administrar su estructura de directorios de WordPress? ¿Cree que WP Migrate DB Pro debería hacer más para que el proceso de migración entre la raíz y el subdirectorio sea más sencillo? ¡Háganos saber sus pensamientos en la sección de comentarios a continuación!

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

About the Author

920dd5ed3b01d7185cc421e15e75ef07

Iain Poulson Product Manager

Iain is a Product Manager based in the South of England. It also manages several WordPress products. He helps people buy and sell WordPress businesses and writes a monthly WordPress trending newsletter.

@polevaultweb polevaultweb.com

ecluYCZdQ2Okuuvd4kIp_MDB-CTA-50

  • 799b5f52a93bf4dd82be35725a1f223bSheiler dit:

    May 15, 2018 at 8:04 am

    Your writing is really concise, clear and helpful!

    Answer

  • d97fb1b65403545f71546591e7b54003Treighton Mauldin dit :

    May 15, 2018 at 8:06 am

    I don’t see a good argument for keeping your local environment so different from production…if you were making it so people could migrate sites that don’t follow the same structure, I could see a lot of problems ensuing.

    Answer

  • 34e9470d89a4e9346326ea203856987dMike Governale dit :

    September 15, 2019 at 02:48

    Hi Jeff, thanks for your article, it’s very clear but I always get a message that my site has a “technical problem” and I can’t display it. I have my staging site in the root directory: beta.wpogoniza.pl and I want to migrate the whole site to another server and put everything in a subdirectory: preprod.wallsstreet.eu/pl/. As you said, I replaced the wp-content links with the path of my sub-directory so: preprod.wallsstreet.eu/pl/ and all the other links I left with the path of the directory root: preprod.wallsstreet.eu. I tried to find app/public but I don’t have that path, I have public_html I tried changing that path to subdirecotry as well and checked both ways but I I keep getting a message that my website has a technical problem. I don’t know if the problem comes from DB or from my index.php or htaccess? I try everything but I can’t solve it.

    Answer

  • 416198b115d7d186a5d899f223fcf61fMaciej Palmowski dit:

    January 23, 2022 at 2:47 p.m.

    Great article. You could also have mentioned Bedrock, which is based on the Composer + subfolders structure.

    Answer

Google Google translator

Google Google Translate


Video Installing wordpress in a 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…