通过重写规则添加.php使自定义404无法访问

时间:2015-08-13 16:43:18

标签: .htaccess

这是我的.htaccess:

DirectoryIndex index.php

ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
ErrorDocument 500 /500.php

php_flag log_errors on
php_value error_log "/www/htdocs/w0123456/errlog/errorlog.txt"

php_flag magic_quotes_gpc off
php_flag register_globals off

<IfModule mod_autoindex.c>
    Options -Indexes
</IfModule>

CheckSpelling off

SetOutputFilter DEFLATE

DirectorySlash Off

Options -MultiViews

<IfModule mod_rewrite.c>
    Options +FollowSymlinks

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,QSA,L]
RewriteRule ^index\.(html?|php)$ http://%{HTTP_HOST}/ [R=301,QSA,L]
RewriteRule ^search$ search.php [NC,L]
RewriteRule ^concat$ concat.php [NC,L]
RewriteRule ^sitemap$ sitemap.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*) $1.php [L]

</IfModule>

它运行正常,但是当我键入一个不存在的URL时,我收到500服务器错误,而不是看到自定义404.php页面。

我需要改变什么?

1 个答案:

答案 0 :(得分:1)

您需要在php扩展规则中添加-f项检查:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCOnd %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]