Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions server/wordpress-in-directory.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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`):

```
Expand All @@ -31,6 +35,22 @@ RewriteRule ^(/)?$ my_subdir/index.php [L]
</IfModule>
```

### 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.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
```

That's all 🙂

## Method II (With URL change)
Expand All @@ -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.1-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/).
Expand Down