0

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)?

  • You can always add custom error pages for diferrent errors: httpd.apache.org/docs/2.2/custom-error.html. But I don't really understand what you are trying to accomplish, as 302 is a "found", not "error". – Lenniey Jan 16 '18 at 14:19
  • @Lenniey I am trying to serve a defined static resource (image in my case) if requested one is not found (instead of returning 404 error) – Maciej Dobrowolski Jan 16 '18 at 14:21
  • OK, let me ask you like this: why would you like to return 200 if a page isn't found? Why won't ErrorDocument 404 suffice? For what you want you'd need rewrites, I presume. Not exactly scripting, but yeah. – Lenniey Jan 16 '18 at 14:25
  • @Lenniey I just have to return 200 for every URL requested, if a one doesn't exist yet, then some default resource should be served instead. There's no possibility of changing a client's logic and it expects me to have every resource it asks me for. – Maciej Dobrowolski Jan 16 '18 at 14:33
1

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.

  • Thank you! Works like a charm! I have also added Options +FollowSymLinks +SymLinksIfOwnerMatch options as they are required to do these checks – Maciej Dobrowolski Jan 16 '18 at 14:42
  • I'm happy to help!:) – Stone Jan 16 '18 at 14:53
0

You seem to be trying to do two mutually contradictory things. When a requested resource is not found, a 200 response would be erroneous - 200 means "everything is okay, I've found what you were looking for and here it is". The 404 response is the correct response, you want to simply use the ErrorDocument, as you mentioned very early in your post, to serve the static resource for anything not found.

  • That's right, it is what I have tried so far - and it's a dirty workaround, no doubt :) The question remains - is there any way to serve a defined static resource if requested one is not found? – Maciej Dobrowolski Jan 16 '18 at 14:17
  • 1
    This is not a dirty workaround - this is the approved way of achieving your desired result. – John Jan 16 '18 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.