K7

K7Blog

须知少年凌云志 曾许人间第一流.
proton
telegram

WordPress Tricks: Multiple WordPress Websites Coexisting in Subdirectories!

Hosting multiple websites in subdirectories in WordPress is actually possible with the official WordPress multisite functionality. However, enabling it may limit some extensibility, which you may not have encountered but I have. Here, I will provide a detailed guide on how to set up a website in a subdirectory.

https://img.k7blog.com/i/2023/03/31/p3bmcx.webp

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

The testing environment here consists of nginx+php7.4+mysql5.7, 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. Let's say your subdirectory site is named "blog," then the code would be:

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

Of course, you need to make this modification in blog/wp-config.php.

Tutorial Start:#

Assuming your website's root directory already has a WordPress site, create a directory named "blog" and upload a new WordPress installation package to it. Access the domain name + "blog" to start the installation!

WordPress now fully supports installation in subdirectories, which was not supported before. Just install it directly.

Note that when filling in the database, you need to modify the database prefix. If you already have a site named "wp," then you should change it to "wp2" or "wp3," and so on.

Rewrite Rules#

Currently, the 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 will be:

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 site, you will need to add the rewrite rules as needed.

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.
For those who are capable, you can also explore additional features such as sharing a user data table between the two sites.

I believe the main application scenario for this is large websites. When the number of articles becomes large, the queries can become slow. By separating each category into a separate website, the data tables become smaller, resulting in faster queries and increased template extensibility.

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