File Not Found Handler

Problem this snippet solves:

This iRule will handle HTTP 404 (File Not Found) errors returned from pool members and return an embedded customized error page.

Let's say you have many servers in your farm and you were told you needed to change the style of your HTTP error response pages? You've got a couple of choices.

1) Manually configure each of your servers with the new content. 2) Build a redirect lookup to issue a HTTP::redirect to a separate server with the error message, but that requires additional hardware. 3) How about embedding the content directly in your iRule?

I vote for #3 and I'll show you how...

This rule creates a global variable in the RULE_INIT event to contain the embedded content. Note I've included variables in there as well - this will come in handy).

Then in the HTTP_REQUEST event, I store the value of the HTTP::uri in a session variable. This HTTP::uri isn't available in the HTTP_RESPONSE event or I'd just reference it there.

And finally in the HTTP_RESPONSE event, I check for a 404 response, and then issue a HTTP::respond with the embedded content. The trick here is to process the content with the TCL "subst" command so that embedded variables will be converted to their values.

Code :

when RULE_INIT {
  set error_404 {

<meta http-equiv="content-type" content="text/html;charset=utf-8" />
404 Not Found

<script>
</script>


iR ul es  Ru le    
[HTTP::status] Error
 

Not Found

The requested URL
$HTTP_URI
was not found on this server.

} } when HTTP_REQUEST { set HTTP_URI [HTTP::uri] } when HTTP_RESPONSE { if { [HTTP::status] == 404 } { HTTP::respond 404 content [subst $::error_404] } }
Updated Jun 06, 2023
Version 2.0

Was this article helpful?

No CommentsBe the first to comment