Forum Discussion

Moinul_Rony's avatar
Moinul_Rony
Icon for Altostratus rankAltostratus
Aug 27, 2013

Redirect to a custom error page when server response is matched with any specific code

Hi I need to respond and redirect on custom response from the server ( tomcat/apache )

 

I have this iRule , just need to verify if the following iRule should work if we wanted to create a respond page and then redirect the traffic to the homepage ?

 

when HTTP_REQUEST {

set hostvar [HTTP::host]

set urivar [HTTP::uri]

set delay 4

}

when HTTP_RESPONSE {

if { [HTTP::status] eq “404 } {

HTTP::respond 200 content \ “

Unfortunately, your request for $hostvar$urivar casued a 404 error. After 4 seconds, you’ll automatically be redirected to our home page. If you feel you’ve tried a valid link, please contact webmaster@sample.com. Sorry for this inconvenience.
” “Content-Type” “text/html”
}

}

Here using HTTP:respond we are holding the user for a delay and then sending the redirect to www.mysite.com.

 

If you can check the code and verify this works that would be great...

 

Thanks.

 

2 Replies

  • JG's avatar
    JG
    Icon for Cumulonimbus rankCumulonimbus

    Syntax does not seem right. See:

     

    https://devcentral.f5.com/wiki/irules.HTTP__respond.ashx

     

  • Can you try this?

    Also, you should consider HTML encoding the host and URI values in the iRule to protect site users from XSS.

    You can do this using string map:

    https://devcentral.f5.com/wiki/iRules.HTML_encoding_proc.ashx
    when HTTP_REQUEST {
        set hostvar [HTTP::host] 
        set urivar [HTTP::uri] 
        set delay 4
    } 
    when HTTP_RESPONSE { 
        if { [HTTP::status] == 404 } {
            HTTP::respond 200 content " Unfortunately, your request for ${hostvar}${urivar} casued a 404 error. After 4 seconds, you’ll automatically be redirected to our home page. If you feel you’ve tried a valid link, please contact webmaster@sample.com. Sorry for this inconvenience. " "Content-Type" "text/html"
        }
    }