May 4, 2020 Network
Configuring Nginx rewrite rules with WordPress Multisite in a subdirectory
This article is suitable for WordPress 5.4 multisite’s file-handling.
Sometimes we'd like to bulid wordpess multisite in a subdirectory. For instance, opening multisite in https://lingdata.org/cn .Because I have uploaded html files in the root directory https://lingdata.org , I just am going to install wordpress in a subdirectory https://lingdata.org/cn and implement accessing https://lingdata.org/cn/nav/ with wordpess multisite network.
Activate WordPress Multisite Network
Just add one line code define('WP_ALLOW_MULTISITE', true);
to wp-config.php
in wordpress directory, like following example:
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*
* For information on other constants that can be used for debugging,
* visit the Codex.
*
* @link https://codex.wordpress.org/Debugging_in_WordPress
*/
define( 'WP_DEBUG', false );
define('WP_ALLOW_MULTISITE', true);
/* That's all, stop editing! Happy publishing. */
Then, goto Wordpress Admin interface like Dashboard → Tools → Network Setup, and follow the instruction to "Create a Network of Wordpess Sites".
Configure website Nginx .conf
file
In website Nginx .conf
file, we will configure the rewrite rules to implement accessing multisite normally.
Beacuse of buliding wordpess multisite in a subdirectory, that is, we already have the .conf
file. We need find and modify it .
The location of website nginx .conf
file depends on your server system. For example, my website .conf
file, is located in /etc/nginx/sites- available/lingdata.conf
, with Debian 10 (the same as Ubutun) server system.
To modify .conf
file, just add two blocks:
# Remeber replace "cn" with your subdirectory
# To remove the “index.php” from your site’s URLs
# in https://lingdata.org/cn/
location /cn/ {
try_files $uri $uri/ /cn/index.php?$args ;
}
# To
if (!-e $request_filename) {
#rewrite /wp-admin$ $scheme://$host$request_uri/ permanent;
rewrite ^/cn(/[^/]+)?(/wp-.*) /cn$2 last;
rewrite ^/cn(/[^/]+)?(/.*\.php) /cn$2 last;
}
That's all.
May 4, 2020
References:
- https://wordpress.org/support/article/nginx/#wordpress-multisite-subdomains-rules
- https://admintuts.tech/server-admin/nginx/how-to-install-and-setup-a-wordpress-multisite-on-nginx/