# Rewrite HTTP response when error code received

**URL:** <https://community.f5.com/t/rewrite-http-response-when-error-code-received/27912>\
**Category:** Technical Forums\
**Tags:** irules, devops, ltm, application-delivery\
**Created:** [January 13, 2015, 12:01pm UTC](https://community.f5.com/t/rewrite-http-response-when-error-code-received/27912 "2015-01-13T12:01:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![LyonsG\_85618](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@LyonsG\_85618](https://community.f5.com/u/LyonsG_85618)\
**Post date:** [January 13, 2015, 12:01pm UTC](https://community.f5.com/t/rewrite-http-response-when-error-code-received/27912/1 "2015-01-13T12:01:06Z")

</div>

Hi

I need to capture 500 errors and when I receive them return a preformated error page. This page will sit on a different domain (where the digital publishing team can update it quickly) and therefore I need to rewrite HOST/URI and send to different pool.

So far I have the following:

```tcl
when HTTP_RESPONSE {
        switch -glob [HTTP::status] {
    "500" {
            HTTP::header replace Host "new.domain.com"
            HTTP::uri "/error"
            pool /TEST/POOL_FOR_NEW_DOMAIN
    }

```

Unfortunately you cannot use HTTP::uri in an HTTP\_RESPONSE.

I do not want to do a 302 redirect as that would mean client would see the new domain.

Is there a simple way round this?

I know we can host page on BIG-IP (ifiles) but as mentioned - publishing team want access to the page to change wording quickly.

Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![Michael\_Jenkins](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@Michael\_Jenkins](https://community.f5.com/u/Michael_Jenkins)\
**Post date:** [January 13, 2015, 1:20pm UTC](https://community.f5.com/t/rewrite-http-response-when-error-code-received/27912/2 "2015-01-13T13:20:46Z")

</div>

Since you can’t access HTTP::uri in the response, you’ll need to set a variable in HTTP\_REQUEST so you can access it.

If you look at the [HTTP::retry](https://devcentral.f5.com/wiki/iRules.HTTP__retry.ashx) command (see the last example on that page), you may be able to _retry_ the request and change the request so you can get a different response from another server. (You may have to add some logic in your HTTP\_REQUEST event to handle whatever host/location you use in the updated request).

If you ever wanted to just return a custom payload from within your iRule, you could use something like [this codeshare entry](https://devcentral.f5.com/wiki/iRules.Convert_404s_to_blank_200s.ashx) to get you started.

Hope this helps

---

<div class="post-metadata">

**Author:** ![LyonsG\_85618](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@LyonsG\_85618](https://community.f5.com/u/LyonsG_85618)\
**Post date:** [January 13, 2015, 1:38pm UTC](https://community.f5.com/t/rewrite-http-response-when-error-code-received/27912/3 "2015-01-13T13:38:57Z")

</div>

Thanks Michael

I do not want to attempt to get response from another server. I just want to redirect/rewrite to the error page.

I dont think HTTP:retry will help with this.

The payload probably wont work either as i am not ‘collecting’ any data.

---

<div class="post-metadata">

**Author:** ![Michael\_Jenkins](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@Michael\_Jenkins](https://community.f5.com/u/Michael_Jenkins)\
**Post date:** [January 13, 2015, 2:08pm UTC](https://community.f5.com/t/rewrite-http-response-when-error-code-received/27912/4 "2015-01-13T14:08:11Z")

</div>

You originally mentioned you don’t want to use a 302 redirect for the client, but want to get the error page from somewhere your publishing team can update quickly, right? In that case, if they need a page to update on a server, the HTTP::retry would allow you to basically do a redirect that the user never sees because it’s all contained in the F5.

Otherwise if you just have custom content to return to the user, I think you can use the

```tcl
HTTP::respond 200 content ""

```

method within HTTP\_RESPONSE to send back some custom html.

---

<div class="post-metadata">

**Author:** ![Brad\_Parker](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@Brad\_Parker](https://community.f5.com/u/Brad_Parker)\
**Post date:** [January 13, 2015, 2:32pm UTC](https://community.f5.com/t/rewrite-http-response-when-error-code-received/27912/5 "2015-01-13T14:32:37Z")

</div>

You could also use a sideband connection to capture the response that is hosted on the other domain and return that as the response in place of the 500 error. [DevCentral - An F5 Technical Community](https://devcentral.f5.com/wiki/irules.SIDEBAND.ashx)

---

<div class="post-metadata">

**Author:** ![LyonsG\_85618](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@LyonsG\_85618](https://community.f5.com/u/LyonsG_85618)\
**Post date:** [January 14, 2015, 3:34pm UTC](https://community.f5.com/t/rewrite-http-response-when-error-code-received/27912/6 "2015-01-14T15:34:42Z")

</div>

Thanks guys - i will have a look at this.

---

<div class="post-metadata">

**Author:** ![Arie](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Arie](https://community.f5.com/u/Arie)\
**Post date:** [January 16, 2015, 9:09pm UTC](https://community.f5.com/t/rewrite-http-response-when-error-code-received/27912/7 "2015-01-16T21:09:07Z")

</div>

Here’s another approach that doesn’t require HTTP::uri. The rule may or may not work as-is since I pulled out environment-specific stuff and inserted your new pool and host. However, it should still provide a template.

```tcl
when HTTP_RESPONSE {

    if { [HTTP::status] equals 403 || [HTTP::status] equals 404 } {

         Retrieve the not-found page if the HTTP status is 403, 404
         Note: Retrieving the page is a valid request, which means that the server will respond with a 200 OK.
               This necessitates that we collect the payload so we can later construct a custom response with
               the proper HTTP response code (404) but with the payload (HTML) from this request.

        if { ![info exists retry] } {

            set retry 1

            HTTP::header replace Host "new.domain.com"
            pool /TEST/POOL_FOR_NEW_DOMAIN
            HTTP::retry "GET /error/not-found/ HTTP/1.1\r\nHost:$requested_host\r\n\r\n"

        }

    }

    if { [info exists is404] } {

        if { [HTTP::status] equals 200 } {

             store the payload of the not-found page if the HTTP status is 200
             (this means that the error page was found on the file system)

            HTTP::collect [HTTP::header Content-Length]

        } else {

             No error page was found in the folder. Construct a generic error page
             and send it to the client.

            log local0.debug "is404 true, but there's no error page on the file system. ([HTTP::status])"

            HTTP::respond 404 content {

                
                    
                        
                    
                

            }

        }

    }

}

when HTTP_RESPONSE_DATA {

     Construct the response with the payload of the not-found page that we collected earlier and
     with the HTTP Response Code "404".

    HTTP::respond 404 content [HTTP::payload] "Content-Type" "text/html"

}

```

---

<div class="post-metadata">

**Author:** ![LyonsG\_85618](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@LyonsG\_85618](https://community.f5.com/u/LyonsG_85618)\
**Post date:** [January 19, 2015, 7:29am UTC](https://community.f5.com/t/rewrite-http-response-when-error-code-received/27912/8 "2015-01-19T07:29:43Z")

</div>

Thnaks for all the help. I have taken a rather simplistic view on this and just done a simple HTTP::respond

```tcl
HTTP::respond 302 noserver Location "https://existing.domain.com/federror"

```

Then in the top part of the iRule i have added:

```tcl
} elseif {($requestedURI starts_with "/error") or ($requestedURI starts_with "/content")} {
      pool /PARTITION/POOL_HOSTING_ERROR_PAGE

```

And this is working.

Thanks

---

<div class="post-metadata">

**Author:** ![Arie](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Arie](https://community.f5.com/u/Arie)\
**Post date:** [January 22, 2015, 7:37pm UTC](https://community.f5.com/t/rewrite-http-response-when-error-code-received/27912/9 "2015-01-22T19:37:59Z")

</div>

While that technically may work, it could result in problems with SEO (Search Engine Optimization).

---

<div class="post-metadata">

**Author:** ![LyonsG\_85618](https://avatars.discourse-cdn.com/v4/letter/l/c89c15/32.png) [@LyonsG\_85618](https://community.f5.com/u/LyonsG_85618)\
**Post date:** [January 23, 2015, 6:54am UTC](https://community.f5.com/t/rewrite-http-response-when-error-code-received/27912/10 "2015-01-23T06:54:48Z")

</div>

Arie - I shoudl have made it clear - the domain will remain the same - its just the request for content that will go to the pool of content servers. So i believe SEO will remain unaltered.
