【发布时间】:2013-07-08 11:53:28
【问题描述】:
我使用以下 URL 重写方案:
example.com/about/
example.com/this/is/a/page/
前往:
example.com/pages/about/about.php
example.com/pages/this/is/a/page/page.php
它工作正常,但是在出现 404 错误时,当输入 example.com/badpage/ 时会显示 404 页面,但会将 URL 字符串更改为 example.com/pages/badpage/badpage.php。
即使出现 404 错误,如何保持 URL 不变?
(从下面的代码可以看出,htaccess 还在请求的 URL 末尾添加了“/”)
htaccesss 代码:
DirectoryIndex /pages/index/index.php
ErrorDocument 404 /pages/error/404.php
RewriteEngine On
#Removes the www from domain for SEO
RewriteCond %{HTTP_HOST} ^www\.portal\.example\.com$ [NC]
RewriteRule ^(.*)$ http://portal.example.com/$1 [L,R=301]
# Don't fix direct file links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://portal.example.com/$1/ [L,R=301]
RewriteRule ^([A-Za-z0-9_-]+)/?$ pages/$1/$1.php [NC,L]
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$ pages/$1/$2/$2.php [NC,L]
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$ pages/$1/$2/$3/$3.php [NC,L]
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$ pages/$1/$2/$3/$4/$4.php [NC,L]
【问题讨论】:
标签: .htaccess url url-rewriting http-status-code-404