WordPress tricks, multiple WordPress websites coexist in subdirectories!

Title: "Creative Ways to Use WordPress: Multiple WordPress Websites in Subdirectories!"
Date: 2023-04-05 07:14:04
Tags: [WordPress, WordPress tutorials]
Published: true
HideInList: false
Feature: https://img.a8dog.com/i/2023/03/31/p3bmcx.webp
IsTop: false

Having multiple WordPress websites in subdirectories is actually possible with the official WordPress multi-site feature. However, enabling this feature may limit some extensibility. You may not have encountered this issue, but I have. Here, I will provide a detailed guide on how to create websites in subdirectories.


I wrote this article on the 29th but haven't published it yet...

For testing purposes, I used nginx+php7.4+mysql5.7, which is a standard installation environment with multiple domain access enabled.
To enable multiple domain access, simply add the following code below define( 'WP_DEBUG', false ); in wp-config.php:

define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);

However, if you want to enable multiple domain access for a subdirectory site, you cannot use this method. For example, if your subdirectory site is named "blog," the code should be:

define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST'] . '/blog');
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST'] . '/blog');

Of course, you need to modify the code in blog/wp-config.php.

Tutorial Starts:#

Assuming you already have a WordPress site in your root directory, create a directory named "blog" and upload a new WordPress installation package to it. Access the domain name + /blog to start the installation!

Currently, WordPress fully supports subdirectory installations, which was not the case before. You can simply install it.

When filling in the database details, you need to modify the database prefix. If you already have a site named "wp," change it to "wp2" or "wp3," and so on.

The current rewrite rules for the main site are:

location /
    try_files $uri $uri/ /index.php?$args;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

After adding our subdirectory site, the rewrite rules become:

location /
    try_files $uri $uri/ /index.php?$args;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location /blog
    try_files $uri $uri/ /blog/index.php?$args;
rewrite /blog/wp-admin$ $scheme://$host$uri/ permanent;

If you have more than one subdirectory site, you need to add the rewrite rules accordingly.

With this setup, the two sites will have separate databases and programs. This scenario can be applied to facilitate communication between different templates without the need for separate management. If you have the capability, you can also explore extending functionalities such as sharing a user database between the two sites.

I believe the main application scenario for this is large websites. When the number of articles becomes large, the site may become slow when querying. By separating each category into a separate website, the database becomes smaller, queries become faster, and there are more options for template customization.

Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.