高级管理文档

为 WordPress 设置独立目录

💡 云策文档标注

概述

本文档指导如何将 WordPress 核心文件安装到子目录中,同时保持网站从根目录提供服务,适用于希望保持根目录整洁的开发者。支持 Apache、nginx 和 IIS 配置。

关键要点

  • 允许 WordPress 安装在子目录(如 /wordpress),而网站 URL 仍为根目录(如 https://example.com)。
  • 提供两种主要方法:不更改站点 URL(方法 I)和更改站点 URL(方法 II),后者涉及修改 WordPress 地址和站点地址设置。
  • 涵盖 Apache (.htaccess)、nginx (server block) 和 IIS (web.config) 的具体配置示例,包括重写规则和服务器块代码。
  • 适用于 WordPress 3.5 及以上版本,包括多站点安装;主题和插件文件仍位于 wp-content 文件夹,不会分离。
  • 包括移动特定文件夹(如 wp-content、插件、主题、上传)的链接参考。

代码示例

Apache (.htaccess) 示例(方法 I):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_subdir/index.php [L] 
</IfModule>

nginx (server block) 示例(方法 I):
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;
}

注意事项

  • 在移动文件时,注意区分复制和移动操作:对于 .htaccess 和 index.php 文件通常复制到根目录,而 IIS 的 web.config 文件需要移动。
  • 更新根目录 index.php 文件中的路径,例如将 require 语句修改为指向子目录的 wp-blog-header.php。
  • 如果使用固定链接,移动后需在 Permalinks 屏幕更新结构,确保 .htaccess 文件有正确权限或手动添加重写规则。
  • 对于多版本安装(如 /latest),可添加重定向规则自动跳转子目录。

📄 原文内容

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, 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.

Moving a Root install to its own directory

Let’s say you’ve installed WordPress at example.com. Now you have two different methods to move WordPress installations into subdirectory:

  1. Without change of SITE-URL (remains example.com)
  2. With change in SITE-URL (it will redirect to example.com/subdirectory)

Method I (Without URL change)

  1. After Installing WordPress in the root folder, move EVERYTHING from the root folder into subdirectory.

Apache (.htaccess)

  1. Create a .htaccess file in the root folder, and put this content inside (just change example.com and my_subdir):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_subdir/index.php [L] 
</IfModule>

nginx (server block)

  1. Add this to your nginx server block:
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)

Moving process

(p.s. If you’ve already installed WP in subdirectory, some steps might be already done automatically).

  1. Create the new location for the core WordPress files to be stored—we will use /wordpress in our examples. On Linux, use mkdir wordpress from your www directory. You’ll probably want to use chown apache:apache on the wordpress directory you created.
  2. Go to the General screen.
  3. In WordPress address (URL): set the address of your main WordPress core files. Example: `https://example.com/wordpress`.
  4. In Site address (URL): set root directory’s URL. Example: `https://example.com`.
  5. Click Save Changes. Do not worry about the errors that happen now! Continue reading.
  6. Now move your WordPress core files (from root directory) to the subdirectory.
  7. Copy (NOT MOVE!) the index.php and .htaccess files from the WordPress directory into the root directory of your site (Blog address). The .htaccess file is invisible, so you may have to set your FTP client to show hidden files. If you are not using pretty permalinks, then you may not have a .htaccess file. If you are running WordPress on a Windows (IIS) server and are using pretty permalinks, you’ll have a web.config rather than a .htaccess file in your WordPress directory. For the index.php file the instructions remain the same, copy (don’t move) the index.php file to your root directory. The web.config file, must be treated differently than the .htaccess file so you must MOVE (DON’T COPY) the web.config file to your root directory.
  8. Open your root directory’s index.php file in a text editor.
  9. Change the following and save the file. Change the line that says:require dirname( __FILE__ ) . '/wp-blog-header.php';to the following, using your directory name for the WordPress core files: require dirname( __FILE__ ) . '/wordpress/wp-blog-header.php';.
  10. Login to the new location. It might now be `https://example.com/wordpress/wp-admin/`.
  11. If you have set up Permalinks, go to the Permalinks Screen and update your Permalink structure. WordPress will automatically update your .htaccess file if it has the appropriate file permissions. If WordPress can’t write to your .htaccess file, it will display the new rewrite rules to you, which you should manually copy into your .htaccess file (in the same directory as the main index.php file).

.htaccess modification

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):

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.

Moving Specific WordPress Folders

The following links explains how to change specific directories within WordPress:

See also