如何缩短WordPress网站路径?将子目录部署的WordPress页面访问路径改为根目录形式
Hey there! Let's get your about page's URL changed from http://localhost:82/_testwebsite/wordpress/about/ to http://localhost:82/about/—here's a step-by-step breakdown to make it happen:
Step 1: Update WordPress Site & Home URLs
First, we need to adjust the core WordPress settings to reflect your desired root URL:
- Log into your WordPress admin dashboard.
- Navigate to Settings > General.
- Keep the WordPress Address (URL) as your actual installation path:
http://localhost:82/_testwebsite/wordpress/(this tells WP where its core files live). - Change the Site Address (URL) to your preferred root URL:
http://localhost:82/(this is what visitors will see in their browser). - Click Save Changes—WordPress will automatically update database entries to match these new URLs.
Step 2: Adjust .htaccess Files
Since WordPress is installed in a subdirectory, we need two .htaccess files to handle the URL rewriting correctly:
Root Directory .htaccess (C:\xampp\htdocs_testwebsite)
If there's no .htaccess file in your root htdocs directory, create a new one and add this code:
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /wordpress/index.php [L] </IfModule>
This tells Apache to route all requests to the WordPress installation in the wordpress subdirectory while keeping the root URL structure.
WordPress Subdirectory .htaccess (C:\xampp\htdocs_testwebsite\wordpress)
Make sure the .htaccess in your WordPress folder has the default rewrite rules (if it doesn't, regenerate it via the permalinks settings later):
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /_testwebsite/wordpress/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /_testwebsite/wordpress/index.php [L] </IfModule>
Step 3: Refresh Permalinks
To ensure WordPress picks up the new URL structure, we need to regenerate the rewrite rules:
- Go back to Settings > Permalinks in the WP admin.
- You don't need to change any settings—just click Save Changes. This will flush the old rewrite rules and apply the new ones.
Step 4: Verify Your Setup
- Visit
http://localhost:82/about/—it should load your custom template page correctly now. - Double-check that other pages/posts also load with the root URL structure (e.g.,
http://localhost:82/your-post/instead of the subdirectory path).
Important Notes
- Ensure Apache's
mod_rewritemodule is enabled: Open your XAMPP Apache config file (httpd.conf), find the line#LoadModule rewrite_module modules/mod_rewrite.so, remove the#at the start, then restart Apache. - Avoid hardcoding URLs in your theme—use WordPress built-in functions like
get_permalink()orhome_url()to generate dynamic links that adapt to your site URL settings. - If you run into 404 errors, double-check your
.htaccessfile permissions (they should be set to 644) and that the rewrite rules are correct.
内容的提问来源于stack exchange,提问作者compliance

