apache.htaccessmod-rewritehttp-status-code-404errordocument

Apache "File not found." instead of 404. error document


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'

Solution

  • 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]