如何将WordPress置于子目录且保留permalinks,静态HTML作首页
Got it, let's tackle this step by step—since the standard Codex method isn't working for your specific setup, we'll use a custom approach that keeps your static Bootstrap homepage at the root, lets WordPress live in /subdirectory, and preserves your original root-level permalinks for all WP pages (except the WP homepage, which stays locked to /subdirectory).
Step 1: Move WordPress to the Subdirectory
First, get your WordPress files into the /subdirectory folder on your server:
- Using FTP or your host's file manager, create a new folder named
subdirectoryin your site's root directory. - Move all your WordPress core files, themes, plugins, and the
wp-contentfolder into thissubdirectoryfolder.
Step 2: Update WordPress Settings
Log into your WordPress dashboard (you'll need to access it temporarily at www.exampledomain.com/subdirectory/wp-admin) and adjust these critical settings:
- Go to Settings > General
- Set WordPress Address (URL) to
www.exampledomain.com/subdirectory(this tells WP where its core files live) - Leave Site Address (URL) as
www.exampledomain.com(this keeps your permalinks rooted at the domain, not the subfolder)
- Set WordPress Address (URL) to
- Save your changes. You might get logged out—just log back in at the
/subdirectory/wp-adminURL.
Step 3: Set Up Your Static Bootstrap Homepage
- In your site's root directory, create (or replace) an
index.htmlfile with your Bootstrap static page content. This will be your default homepage. - Ensure your server prioritizes
index.htmlover WordPress'sindex.php:- For Apache: Add this line to the top of your root
.htaccessfile:DirectoryIndex index.html index.php - For Nginx: Update your server block's
indexdirective to:index index.html index.php;
- For Apache: Add this line to the top of your root
Step 4: Configure .htaccess Rules (The Magic Part)
This is where we route traffic correctly so your static homepage loads at the root, WP's homepage stays at /subdirectory, and all other WP pages load with root-level permalinks.
Root Directory .htaccess
Replace or update your root .htaccess with this code:
# Prioritize static homepage DirectoryIndex index.html index.php RewriteEngine On RewriteBase / # Load static homepage for root request RewriteRule ^$ index.html [L] # Allow direct access to the WordPress subdirectory RewriteRule ^subdirectory/ - [L] # Route all other non-file/non-directory requests to WordPress in subdirectory RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /subdirectory/index.php [L]
WordPress Subdirectory .htaccess
Go to the /subdirectory folder and make sure its .htaccess has this code (if it doesn't, update it):
RewriteEngine On RewriteBase /subdirectory/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /subdirectory/index.php [L]
Step 5: Verify & Fix Permalinks
- Test your setup:
www.exampledomain.comloads your Bootstrap static homepagewww.exampledomain.com/subdirectoryloads your WordPress homepagewww.exampledomain.com/somepageloads your WordPress page without the/subdirectoryprefix
- If permalinks are broken, head to WP Admin > Settings > Permalinks and click "Save Changes" (no need to edit anything—this refreshes WP's permalink rules).
Pro Tip
Always back up your .htaccess files and WordPress database before making these changes, just in case you need to roll back.
内容的提问来源于stack exchange,提问作者Greg Viv

