Httpd – Apache httpd serve a 200 response with static resource if requested file is not found

apache-2.2Apache2http-status-code-404httpdhttpd.conf

I am looking for a way to configure httpd server to respond with an image if requested file is not found. I know it can be simply done by:

ErrorDocument 404 "/static/thumbnails/404.png"

This one works, but a server responds with 404 code and it is something that I am trying to get rid of (ideally if a server would return 200).

I have tried doing a simple redirect as follows:

Redirect "for/sure/doesnt/exist" "/static/thumbnails/blank-thumbnail.png"
ErrorDocument 404 "/for/sure/doesnt/exist"

But it doesn't work (and even if it worked – it would return 302 response) as it's what is returned for GET http://mydomain/static/thumbnails/xxxx.png:

Not Found The requested URL /static/thumbnails/any was not found on
this server.

Additionally, a 302 Found error was encountered while trying to use an
ErrorDocument to handle the request.

What is interesting, Apache log says that status code for this response is 302:

[16/Jan/2018:08:39:56 -0500] "GET /static/thumbnails/any HTTP/1.1" 302 – "-" "curl/7.47.0"

So it looks that Redirect actually worked, but I am also sure that "/static/thumbnails/blank-thumbnail.png" path is correct.

Is there any way, without scripting, to serve a defined static resource (an image in my case) if requested one is not found (instead of returning 404)?

Best Answer

Maybe the solution in this: https://stackoverflow.com/questions/5190206/how-do-i-redirect-a-url-that-isnt-found-without-sending-a-404-header can help you.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

This rewrites everything if that is not a file/dir/link. In the last step you can put anything. And the server will server it as 200.