Apache “File not found.” instead of 404. error document

倖福魔咒の 提交于 2020-01-14 14:33:08

问题


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

First, I have two RewriteRules in my .htaccess:

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

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

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

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?

Thanks for your help!

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 Error Log:

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

回答1:


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]


来源:https://stackoverflow.com/questions/37898054/apache-file-not-found-instead-of-404-error-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!