求助:使用.htaccess实现伪静态URL时出现404错误
Hey there! Let's get your pretty URLs working and fix that frustrating 404 error. You're aiming to convert website.com/project-details.php?Residential/The-Serenas/1 to the cleaner, more user-friendly website.com/Residential/The-Serenas/1—that's a great move for both UX and SEO. Let's break down what's missing in your current .htaccess setup and how to fix it.
Common Issues in Your Current Code
From what you shared, a couple of key pieces are likely causing the 404:
- Your
RewriteBase /basename/is probably incorrect unless your site is hosted in a subdirectory namedbasename. If your site lives directly at the root of your domain (e.g.,website.com/), this should be set to/instead. - You didn't include the actual rewrite rules that map the pretty URL back to your
project-details.phpfile—right now, the server doesn't know what to do with the clean URL path.
Corrected .htaccess Code
Replace your current .htaccess content with this:
Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase / # External redirect: Send users from the old URL to the pretty one (prevents duplicate content) RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/project-details\.php\?([^\s]+)\s [NC] RewriteRule ^ /%1? [R=301,L] # Internal rewrite: Map the pretty URL back to the actual PHP file RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /project-details.php?$1 [L]
What Each Part Does
Let's break down the key lines so you understand exactly what's happening:
Options +FollowSymLinks -MultiViews: Enables symbolic link following and disables MultiViews (which can interfere with rewrite rules by auto-resolving file extensions).RewriteBase /: Sets the base directory for rewrite rules—use this if your site is at the domain root. If you're in a subdirectory likewebsite.com/my-project/, change this to/my-project/.- External Redirect Rule: This catches requests to the old
project-details.php?xxxURL and redirects users to the pretty version. TheR=301is a permanent redirect (good for SEO), and the?at the end strips any leftover query string. - Internal Rewrite Rules: The two
RewriteCondlines ensure we don't rewrite actual files or directories (so if you have a file namedResidential.html, it won't be affected). The final rule takes any request that isn't a file/directory and passes it as the query string toproject-details.php.
Additional Checks to Ensure It Works
- Make sure your web server has the
mod_rewritemodule enabled (most shared hosts have this on by default, but you can confirm with your host if unsure). - Verify your
.htaccessfile is in the root directory of your site (whereproject-details.phpis located). - Check file permissions:
.htaccessshould be set to644(readable by the server, writable only by you).
If you're still seeing 404s after this, double-check that project-details.php is in the correct location and that your server allows overrides via .htaccess (look for AllowOverride All in your server config for the site directory).
内容的提问来源于stack exchange,提问作者Inderjeet

