Apache“找不到文件”。而不是404.错误文件

时间:2016-06-18 14:26:57

标签: apache .htaccess mod-rewrite http-status-code-404 errordocument

我想设置自定义404错误文档,但我遇到了问题!

首先,我的.htaccess中有两个RewriteRules:

RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /$2/$3.php?lang=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/?$ /$2/$3/$4.php?lang=$1 [L]

所以我的网址如下:/ de-de / page / other / stuff and opend /page/other/stuff.php?lang=de-de

现在是时候设置我自己的错误文档了: ErrorDocument 404 /404.php

如果“/ de-de / page / other /”中出现错误,例如“/ de-de / wrong / page /”,我的错误文档成功了。但是如果php文件名中有错误(例如“错误的东西”而不是“东西”),我只会收到消息“找不到文件”。但不是我的错误文件。

有什么问题?我该怎么做才能解决它?

感谢您的帮助!

AuthType Basic
AuthName "Password Protected Area"
AuthUserFile /var/www/vhosts/<domain>/development/tests/demo.<domain>/.htpasswd
Require valid-user

<IfModule mod_rewrite.c>
    RewriteEngine on        
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,QSA]
</IfModule>



RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /$2/$3.php?lang=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/?$ /$2/$3/$4.php?lang=$1 [L]

ErrorDocument 404 /404.php 

Apache错误日志:

[404] GET /de-de/support/support/fdfssdf HTTP/1.0
AH01071: Got error 'Primary script unknown\n'

1 个答案:

答案 0 :(得分:0)

使用RewriteCond检查php文件是否存在:

RewriteCond %{REQUEST_URI} ^/([^/.]+)/([^/.]+)/([^/.]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%2/%3.php -f
RewriteRule ^ /%2/%3.php?lang=%1 [L]

RewriteCond %{REQUEST_URI} ^/([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%2/%3/%4.php -f
RewriteRule ^ /%2/%3/%4.php?lang=%1 [L]