I have a relatively simple Apache httpd + Tomcat via mod_jk setup that works well, but I need to have Tomcat handle certain 404 conditions. Basically, the httpd service will handle images and PDF files and the occasional IIS hacking attempt. However the PDF files are actually created by Tomcat and deleted from the filesystem later, and some people have placed links to those volatile PDF documents out on the web. This results in 404 messages, when what I really want is to have the user see the form where the PDF was generated from.

I can, of course, have Tomcat handle all 404s:

ErrorDocument 404 /404.jsp

But there are too many of them, and Tomcat just sees 404.jsp, it doesn't know what the original URL was. What I need is:

  1. httpd continues to serve up all images, PDFs, etc.
  2. if a PDF file is missing, send that request through mod_jk. For all other types, it continues to respond with its own 404.
  3. Tomcat figures out where the PDF really came from and forwards the user on to that page.

Is this doable without too much .conf surgery?


You could make smart 404 jsps that use look at the referring url and redirect the request based on some logic in the jsp, or redirect via javascript. If the referrer has .pdf in the url, parse the url and redirect the user to the proper form, assuming you can lookup the form you need based on the pdf requested.


If the PDF is actually generated on the webserver and sits there for a while to be served from apache, then try using mod_rewrite on all non-existing files that end in ".pdf" and rewriting the request to jsp. Assuming you don't create the file with the same name again (in that case don't use a permanent redirect) (Also assuming that you don't have URLs of the form /foo.pdf?specialjuice=1 since the RewriteRule below will lose the special juice):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\.pdf)$ /generateform.jsp?oldurl=$1 [R=301,L]

Anything else that doesn't exist should get the regular 404 handler.

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.