1

I have a Directory and DirectoryMatch declaration with ErrorDocument as

<Directory "/Images/folder">
        Options FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all

        ErrorDocument 404 /folder/default.jpg
</Directory>

This works fine and I get a 404 with the default image in the browser. But changing 404 to 302 still returns a 404 in place of intended 302. Only this time the default image is not returned with a 404.

I am using apache 2.2. Please advise.

  • 2
    302 redirects don't have a document associated with them. What are you actually trying to do? – Stephen Ostermiller May 1 at 21:38
3
ErrorDocument 404 /folder/default.jpg

Your explanation appears to show a misunderstanding of what this directive does. This particular directive will return /folder/default.jpg for any request (in this directory tree) that results in a 404, ie. does not exist. It defines the 404 error document.

Changing this to 302 does not make sense. You are simply removing the 404 error document (a 404 status is still returned but without the intended error document, ie. default.jpg). 3xx status are not error states, they are "redirection" states and require a corresponding Location HTTP response header.

By the sounds of it you perhaps want to "redirect" to the /folder/default.jpg image when an image that does not exist is requested? Instead of triggering a 404? For this you could specify an absolute URL (as noted by @Stephen in comments), or use mod_rewrite RewriteRule and RewriteCond. However, using the 404 error document in the conventional way would be the preferred approach here I think.

Reference:

  • 1
    If you want to redirect to the image when something isn't found, you could change the error document to a full URL: ErrorDocument 404 http://example.com/folder/default.jpg. That changes it to a redirect according to the Apache documentation – Stephen Ostermiller May 2 at 1:03
  • @StephenOstermiller Ah, yes! (Although still probably not what should be done in this instance. Redirecting to the "error document" is usually a last resort, when it resides on a different host.) – MrWhite May 2 at 5:26
  • 1
    Thanks for answering. Yes, I agree my knowledge is primitive and the 404 solution was my first resort. Intention was to return the default image but the end user should see the same url(no redirection). The default path and image are not intended to be exposed. The reason I tried the "hack" of forcing a non-404 code was a desperate measure because one of the embedded device couldn't display the default image and barfed at 404. Browser (<img> tag), however, worked as expected. While I was awaiting an answer, I resorted to RewriteCond and RewriteRule albeit with a 200 to support the one off. – deepSymmetry May 2 at 19:34
  • "RewriteCond and RewriteRule albeit with a 200" - Ah OK, that will be an internal rewrite. However, that could potentially run the risk of these non-existent images being picked up by Google image search (same image, ie. default.jpg, but with multiple filenames.) - if that is a concern. This could be prevented by sending an X-Robots-Tag: noindex HTTP response header when default.jpg is served. The fact that the "embedded device" failed to display the default image might have been due to incorrect HTTP response headers? – MrWhite May 2 at 21:45

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.