diff --git a/server/wordpress-in-directory.md b/server/wordpress-in-directory.md index b9da005..e70e2c7 100644 --- a/server/wordpress-in-directory.md +++ b/server/wordpress-in-directory.md @@ -2,6 +2,8 @@ Many people want WordPress to power their website's root (e.g. https://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. +**Note:** This guide covers configuration for Apache (`.htaccess`), nginx (server blocks), and IIS (`web.config`). + As of [Version 3.5](https://wordpress.org/documentation/wordpress-version/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. @@ -16,6 +18,8 @@ Let's say you've installed WordPress at `example.com`. Now you have two differen ## Method I (Without URL change) 1. After Installing WordPress in the root folder, move EVERYTHING from the root folder into subdirectory. + +### Apache (.htaccess) 2. Create a `.htaccess` file in the root folder, and put this content inside (just change `example.com` and `my_subdir`): ``` @@ -31,6 +35,22 @@ RewriteRule ^(/)?$ my_subdir/index.php [L] ``` +### nginx (server block) +2. Add this to your nginx server block: + +```nginx +location /my_subdir/ { + try_files $uri $uri/ /my_subdir/index.php?$args; +} + +location ~ \.php$ { + fastcgi_split_path_info ^(.+\.php)(/.*)$; + fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + include fastcgi_params; +} +``` + That's all 🙂 ## Method II (With URL change) @@ -55,12 +75,31 @@ _(p.s. If you've already installed WP in subdirectory, some steps might be alrea In some cases, some people like to install separate versions in a subdirectory (such as `/2010`, `/2011`, `/latest` and etc..), and want that website (by default) used the latest version, then Install WordPress in a subdirectory, such as `/my_subdir` and in your root folder's .htaccess file add the following (just change the words as you need): +**Apache (.htaccess):** ``` RewriteEngine On RewriteCond %{HTTP_HOST} ^(www.)?example.com$ RewriteRule ^(/)?$ my_subdir\[L\] ``` +**nginx (server block):** +```nginx +location = / { + return 301 /my_subdir/; +} + +location /my_subdir/ { + try_files $uri $uri/ /my_subdir/index.php?$args; +} + +location ~ \.php$ { + fastcgi_split_path_info ^(.+\.php)(/.*)$; + fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + include fastcgi_params; +} +``` + Now when users to go your root domain (`example.com`), it will automatically redirect to the subdirectory you specified. Note: This code comes from Site 5's post here: [How to Redirect Your Domain to a Subfolder Using .htaccess](https://qa.site5.com/advanced/how-to-redirect-your-domain-to-a-subfolder-using-htaccess/).