Apache本地开发环境下非纯固定链接返回404问题求助
Hey there, let's work through this permalink issue you're hitting with your local XAMPP setup. I've dealt with similar headaches when switching to "Post name" permalinks, so here are the most common fixes to get your pages working again:
Enable mod_rewrite in Apache
This is the #1 culprit for permalink 404s on XAMPP. Here's how to turn it on:- Open your XAMPP Control Panel, click the "Config" button next to Apache, then select
httpd.conf. - Search for the line starting with
#LoadModule rewrite_module modules/mod_rewrite.so— remove the#at the beginning to uncomment it. - Save the file, then restart Apache from the XAMPP Control Panel.
- Open your XAMPP Control Panel, click the "Config" button next to Apache, then select
Fix your .htaccess file
WordPress needs a properly configured.htaccessto handle "Post name" permalinks. Try these steps:- First, back up your existing
.htaccessfile (if it exists) in your WordPress root directory. - Go to your WordPress admin dashboard → Settings → Permalinks. Switch back to "Plain" permalinks, save changes, then switch back to "Post name" and save again. This forces WordPress to regenerate the correct rewrite rules.
- If WordPress can't write to
.htaccessautomatically (common on local setups), add these rules manually:
Note: If your WordPress site is in a subfolder (e.g.,# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPresslocalhost/my-site), updateRewriteBase /toRewriteBase /my-site/and change the last line toRewriteRule . /my-site/index.php [L].
- First, back up your existing
Verify custom post type rewrite settings
Even though your custom post types are working, a misconfiguredrewriteparameter could interfere with other pages. Check your custom post type registration code (usually infunctions.phpor a custom plugin):register_post_type('your_cpt_slug', array( // ... other settings 'rewrite' => array( 'slug' => 'your-cpt-slug', // Make sure this doesn't clash with page/post slugs 'with_front' => true // Set to false only if you want to skip the base permalink structure ), // ... ));If you made changes here, save the file and go back to Settings → Permalinks to resave your settings (this flushes rewrite rules).
Check file permissions
On some systems, WordPress might not have permission to write to.htaccess.- For Windows: Right-click the
.htaccessfile → Properties → Security and give your user account "Write" permissions. - For Mac/Linux: Open Terminal, navigate to your WordPress root folder, and run
chmod 664 .htaccessto set the correct permissions.
- For Windows: Right-click the
Flush rewrite rules manually
Sometimes WordPress caches old rewrite rules. If the above steps don't work, add this line temporarily to your theme'sfunctions.phpfile, load any page on your site, then remove it:flush_rewrite_rules();Important: Don't leave this line in permanently — it runs on every page load and will slow down your site.
Most of the time, enabling mod_rewrite and fixing .htaccess resolves this issue. If you're still seeing 404s, double-check that none of your page slugs conflict with custom post type slugs or WordPress default routes.
内容的提问来源于stack exchange,提问作者Cactusman07

