{"id":183,"date":"2009-02-13T11:12:07","date_gmt":"2009-02-13T16:12:07","guid":{"rendered":"http:\/\/www.webadminblog.com\/?p=183"},"modified":"2009-07-07T12:26:30","modified_gmt":"2009-07-07T17:26:30","slug":"customizing-apache-error-codes-by-url","status":"publish","type":"post","link":"https:\/\/www.webadminblog.com\/index.php\/2009\/02\/13\/customizing-apache-error-codes-by-url\/","title":{"rendered":"Customizing Apache Error Codes By URL"},"content":{"rendered":"<p>I&#8217;ve had a couple of discussions lately about customized Apache error pages that prompted me to do a little bit of research on it.\u00a0 What I&#8217;ve come up with is somewhat interesting so I thought I&#8217;d share it with everyone.\u00a0 First, it is not technically possible to tell Apache to serve up a different error page for image content than for html content than for php content since the only command Apache accepts for this is of the &#8220;ErrorDocument error-code document&#8221; format.\u00a0 That said, if you allow .htaccess overrides on a particular directory, then you can specify your ErrorDocument directive in there as well; overriding the default error handling specified in the httpd.conf file.\u00a0 An example:<\/p>\n<p>In my httpd.conf file I have all 404&#8217;s going to errorpage.cgi with the following line:<\/p>\n<p>ErrorDocument 404 \/cgi-bin\/errorpage.cgi<\/p>\n<p>I&#8217;m a good little code monkey and put all of my images in a \/images directory under the DirectoryRoot.\u00a0 By default, if I were to hit a non-existent image in that directory, I would get the default error message defined in the httpd.conf file.\u00a0 If that image were referenced in an html page that I hit, I now download the html page plus the errorpage.cgi page for the bad image reference, introducing one whole page&#8217;s worth of additional overhead.<\/p>\n<p>But since I was a good code monkey and put all of my images in a \/images directory, the fix for this is really simple.\u00a0 I create a .htaccess file inside of my \/images directory and add the following line to it:<\/p>\n<p>ErrorDocument 404 &#8220;404 &#8211; Image does not exist\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;&#8211; Note: No end quote is intentional<\/p>\n<p>Now, if I hit http:\/\/www.mysite.com\/badpage.html I get the errorpage.cgi page, but if I hit http:\/\/www.mysite.com\/images\/badimage.jpg I get a short and sweet message saying &#8220;404 &#8211; Image does not exist&#8221;.\u00a0 I haven&#8217;t tested this yet to see how it works when you are using something like mod_oc4j to send certain URLs to an application server, but it&#8217;s possible that this could work there too if Apache checks for existing static URLs before passing requests to the app server.\u00a0 Further testing could be useful there.<\/p>\n<p>So there you have it.\u00a0 I can&#8217;t tell Apache to serve up different error pages based on the URL or file type, but if I&#8217;m diligent about putting different files under different directories, I can effectively do the same thing using .htaccess files.\u00a0 Woot!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve had a couple of discussions lately about customized Apache error pages that prompted me to do a little bit of research on it.\u00a0 What I&#8217;ve come up with is somewhat interesting so I thought I&#8217;d share it with everyone.\u00a0 First, it is not technically possible to tell Apache to serve up a different error [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[96],"tags":[216],"class_list":["post-183","post","type-post","status-publish","format-standard","hentry","category-software-and-tools","tag-apache-errordocument-custom-error-page-htaccess-content-file-type-url"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pfI0c-2X","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/posts\/183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=183"}],"version-history":[{"count":2,"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/posts\/183\/revisions"}],"predecessor-version":[{"id":288,"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/posts\/183\/revisions\/288"}],"wp:attachment":[{"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webadminblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}