# custom error page based on /URI

**URL:** <https://community.f5.com/t/custom-error-page-based-on-uri/44628>\
**Category:** Technical Forums\
**Tags:** irules, devops, application-delivery\
**Created:** [September 25, 2013, 12:46am UTC](https://community.f5.com/t/custom-error-page-based-on-uri/44628 "2013-09-25T00:46:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moinul\_Rony](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/moinul_rony/32/7350_2.png) [@Moinul\_Rony](https://community.f5.com/u/Moinul_Rony)\
**Post date:** [September 25, 2013, 12:46am UTC](https://community.f5.com/t/custom-error-page-based-on-uri/44628/1 "2013-09-25T00:46:18Z")

</div>

Hi I have a iRule on our application server that is catching all 4xx and 5xx error responses and responds with a custom response using the below iRule. We have some application that has inbuilt error page set up and we don’t want to put the iRule on those apps. So what I am looking for is to implement the existing iRule based on /URI

```tcl
when HTTP_REQUEST {
    set http_request [HTTP::request]
    set appvar [HTTP::cookie JSESSIONID]
    set http_request_time [clock clicks -milliseconds]
    set LogString "Client [IP::client_addr]:[TCP::client_port] -> [HTTP::host][HTTP::uri]"
    set delay 4
} 
when HTTP_RESPONSE { 
     if { ([HTTP::status] starts_with "4") || ([HTTP::status] starts_with "5")} {
        HTTP::respond 200 content " MySite\
        Oops! Something went wrong. Please try again.\
        Sorry for this inconvenience. " "Content-Type" "text/html"
   log local0. "$LogString (response) - pool info: [LB::server] - status: [HTTP::status] (request/response delta: [expr {[clock clicks -milliseconds] - $http_request_time}] ms)"
        log local0. "Session ID was $appvar"
    }
}

```

Thanks - Rony

---

<div class="post-metadata">

**Author:** ![Kevin\_Stewart](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/kevin_stewart/32/15267_2.png) [@Kevin\_Stewart](https://community.f5.com/u/Kevin_Stewart)\
**Post date:** [September 25, 2013, 3:17am UTC](https://community.f5.com/t/custom-error-page-based-on-uri/44628/2 "2013-09-25T03:17:18Z")

</div>

Without rebuilding your entire iRule, the logic for such a requirements might look like this:

```tcl
when HTTP_REQUEST {
    if { [string tolower [HTTP::uri]] starts_with "/uri" } {        
        set noerror 1
    }
}    
when HTTP_RESPONSE {    
    if { ( ( [HTTP::status] starts_with "4" ) or ( [HTTP::status] starts_with "5" ) ) and not ( [info exists noerror] ) } {        
         HTTP::respond 200 content "..."   
    }
}

```

This will set a variable in the HTTP\_REQUEST event if a specific URI is requested, which will be visible in the corresponding HTTP\_RESPONSE event. If users might flip between applications in a single connection, you can optionally add the following to the bottom of the HTTP\_RESPONSE event to clear the variable between requests if it exists:

```tcl
if { [info exists noerror] } { unset noerror }

```
