Apache“未找到文件."而不是 404. 错误文档 [英] Apache "File not found." instead of 404. error document

查看:104
本文介绍了Apache“未找到文件."而不是 404. 错误文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to set a custom 404 error document, but I have a problem with it!

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

First, I have two RewriteRules in my .htaccess:

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

所以我的 URL 看起来像:/de-de/page/other/stuff 并打开/page/other/stuff.php?lang=de-de

So my URL looks like: /de-de/page/other/stuff and opend /page/other/stuff.php?lang=de-de

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

Now it's time to set my own error document: ErrorDocument 404 /404.php

如果/de-de/page/other/"中有错误,例如/de-de/wrong/page/",我成功获取了错误文档.但如果 php 文件名有误(例如wrong-stuff"而不是stuff"),我只会收到消息找不到文件".但不是我的错误文档.

If there is a mistake in "/de-de/page/other/" for example " /de-de/wrong/page/" I got the error document successfull. But if there is a mistake in the php file name (for example "wrong-stuff" instead of "stuff") I only get the message "File not found." but not my error document.

怎么了?我能做些什么来修复它?

What is wrong? What can I do to fix it?

感谢您的帮助!

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 错误日志:

Apache Error Log:

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

推荐答案

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

Use RewriteCond to check if the php file exists:

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]

这篇关于Apache“未找到文件."而不是 404. 错误文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆