i'm getting a little bit frustrated, so any help would be appreciated. Is it possible to catch a 404/directory not found, combined with a FilesMatch-block in .htaccess?
I created the following .htacces in order to rewrite non existing .json-files to a 204 No Content (preventing errors when using $.getJSON
):
<FilesMatch "\.(json)$">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ - [R=204]
</FilesMatch>
I tested some things with the following files/directories-structure:
httpdocs
- a
- a.json
- b
The .htaccess works when I request a non-existing configuration-file (b/b.json
), but when I try to load a configuration-file of a non-existing directory (c/c.json
), a 404 statusCode will be returned instead of the desired 204.
Requesting a/a.json
gives httpStatusCode 200 (Found, which is correct)
Requesting b/b.json
gives httpStatusCode 204 (No content, also correct, handled by the .htaccess)
Requesting c/c.json
gives httpStatusCode 404 (Not Found)
I want request c to return a 204 No Content as well. I've tried some thing like catching the directory from the request using <FilesMatch .*\/(.*)\.(json)$>
and using the $1
for the -d instead of the %{REQUEST_FILENAME}
, but this doesn't work.
I also tried to catch the 404-error using the ErrorDocument, only for .json-files, but this doesn't seam to work. The redirect won't work and a 404 statusCode will be returned.
<FilesMatch "\.(json)$">
ErrorDocument 404 /204.php
</FilesMatch>
What am I doing wrong? Any ideas how to catch this?