You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:使用.htaccess实现伪静态URL时出现404错误

Fixing 404 Error with Pretty URL Rewrites for project-details.php

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 named basename. 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.php file—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 like website.com/my-project/, change this to /my-project/.
  • External Redirect Rule: This catches requests to the old project-details.php?xxx URL and redirects users to the pretty version. The R=301 is a permanent redirect (good for SEO), and the ? at the end strips any leftover query string.
  • Internal Rewrite Rules: The two RewriteCond lines ensure we don't rewrite actual files or directories (so if you have a file named Residential.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 to project-details.php.

Additional Checks to Ensure It Works

  • Make sure your web server has the mod_rewrite module enabled (most shared hosts have this on by default, but you can confirm with your host if unsure).
  • Verify your .htaccess file is in the root directory of your site (where project-details.php is located).
  • Check file permissions: .htaccess should be set to 644 (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:59:09