WordPress站点.htaccess配置:主页映射至main子目录,其余页面保留根目录
Got it, let's work through this .htaccess setup to get exactly what you need: serving your new /main WordPress homepage at example.com (no URL redirect) while keeping all other pages like example.com/about-us pointing to your root WordPress site.
Step 1: Update Root .htaccess
First, replace or modify your root directory's .htaccess file with these rules. This will prioritize the new homepage forward while letting all other requests fall back to your existing root site:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / # Serve the new homepage from /main (internal rewrite, no URL change) RewriteRule ^$ /main/ [L] # Default WordPress rules for all other requests (uses root site) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
Step 2: Fix WordPress Configuration in /main
To make sure your new /main WordPress site loads resources (CSS, JS, images) correctly without pointing to /main in URLs, add these lines to your /main/wp-config.php file (right before the line that says /* That's all, stop editing! Happy publishing. */):
define('WP_HOME','https://example.com'); define('WP_SITEURL','https://example.com/main');
This tells the /main WordPress installation that its public-facing URL is the root domain, even though its core files live in the subdirectory.
Step 3: Test and Verify
- Visit
example.com: You should see your new/mainhomepage, with the URL still showingexample.com(no redirect to/main). - Visit
example.com/about-us(or any other existing page): You should land on your original root WordPress site's page. - Check the new homepage's source code to ensure all asset links (CSS, JS) point to
https://example.com/wp-content/...instead of including/main(the wp-config.php changes take care of this).
Key Notes
- Make sure your server has
mod_rewriteenabled (most Apache hosts have this on by default, but if not, you may need to contact your host or enable it via cPanel). - If you have existing custom rules in your root .htaccess, merge these carefully—keep the homepage rewrite rule above the default WordPress index.php rule.
内容的提问来源于stack exchange,提问作者user2626709

