# HTTP 404 ERROR HANDLING through IRULE

**URL:** <https://community.f5.com/t/http-404-error-handling-through-irule/48003>\
**Category:** Technical Forums\
**Tags:** tmsh, tmos, irules, big-ip, devops, ltm, application-delivery\
**Created:** [March 10, 2021, 2:40am UTC](https://community.f5.com/t/http-404-error-handling-through-irule/48003 "2021-03-10T02:40:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deepak\_Nair](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@Deepak\_Nair](https://community.f5.com/u/Deepak_Nair)\
**Post date:** [March 10, 2021, 2:40am UTC](https://community.f5.com/t/http-404-error-handling-through-irule/48003/1 "2021-03-10T02:40:59Z")

</div>

Hello Experts ,

I am trying to build an irule which will send a HTTP 404 error CODE if the user intentionally or mistakenly typed a wrong URL .

Below is the IRULE that i have written :

```tcl
when HTTP_REQUEST { 
  if {([string tolower [HTTP::host]] equals "xyz.APAC.com") && [HTTP::uri] starts_with "/ARCHIAC" } {
    HTTP::uri [string map [list "/archiac/" "/arcgis/"] [HTTP::uri]]
    pool PL_BNE_ENT_6443
    return
  } 
  if {([string tolower [HTTP::host]] equals "xyz.APAC.com") && [HTTP::uri] starts_with "/archiacportal" } {
    pool PL_BNE_ENT_arcgisportal_443
    return
  }
  if {[HTTP::uri] matches_regex ".*" } {
    HTTP::respond 404 content {Page Not Found Page Not Found} 
  } 
}

```

This irule work fine for most of the cases . BUT if any user start the URI with /ARCHIACjkaejksfsdjkhdfsj , this request will be send to server .

Same is TRUE with /archiacportalsdfjkbasdjkfbsd .

If there any way to discard the junk characters with HTTP 404 error when someone start the URI with above STRINGS ???

---

<div class="post-metadata">

**Author:** ![JRahm](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/jrahm/32/3327_2.png) [@JRahm](https://community.f5.com/u/JRahm)\
**Post date:** [March 10, 2021, 8:04pm UTC](https://community.f5.com/t/http-404-error-handling-through-irule/48003/2 "2021-03-10T20:04:45Z")

</div>

/ARCHIACjkaejksfsdjkhdfsj matches your first starts\_with condition. Can you give a couple more examples of good matches versus bad matches? I’m not sure I have a clear enough picture of what is “good” to provide you a proper solution. On top of that, a few comments:

- I’m not sure how anything gets through to the server with your HTTP::host. If you string tolower that, it’ll never match [xyz.APAC.com](http://xyz.APAC.com). I’m guessing that’s just a sanitization error for adding to DC?
- You don’t need a string tolower on the HTTP::host. That is case insensitive by RFC.
- You almost certainly do not want matches\_regex there. Recommendation would be to use if, elseif, else strategy here, so by virtue of not matching your first two cases, the else will handle your 404 for everything else.

---

<div class="post-metadata">

**Author:** ![Deepak\_Nair](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@Deepak\_Nair](https://community.f5.com/u/Deepak_Nair)\
**Post date:** [March 13, 2021, 4:08am UTC](https://community.f5.com/t/http-404-error-handling-through-irule/48003/3 "2021-03-13T04:08:30Z")

</div>

Hi Jason Rahm ,

I have re-written my irule using some variable for flexible matching :

Here is irule that i am using :

when HTTP\_REQUEST {

set host [string tolower [HTTP::host]]

set uri [HTTP::uri]

if {$host equals “[xyz.apac.com](http://xyz.apac.com/).”} {

set context [getfield $uri “/” 2]

if {($context ne “archiacserver”) and ($context ne “archiacportal”) and ($context ne “archiac”)} {

HTTP::respond 404 content {Page Not Found Page Not Found}

} elseif { ($uri starts\_with “/archiacserver”)} {

HTTP::uri [string map {“/archiacserver” “/archiac”} [HTTP::uri]

HTTP::redirect “https://$host[HTTP::uri]”

pool PL\_BNE\_ENT\_6443

return

} elseif {$uri starts\_with “/archiac”} {

pool PL\_BNE\_ENT\_6443

}elseif {$uri starts\_with “/archiacportal”} {

pool PL\_BNE\_ENT\_archiacportal\_443

return

}

}

}

SO below are GOOD matching :

[www.xyz.apac.com/archiacserver](http://www.xyz.apac.com/archiacserver)

[www.xyz.apac.com/archiacserver/](http://www.xyz.apac.com/archiacserver/)

[www.xyz.apac.com/archiacserver/abcde/efgh](http://www.xyz.apac.com/archiacserver/abcde/efgh)

[www.xyz.apac.com/archiacportal](http://www.xyz.apac.com/archiacportal)

BAD MATCH

[www.xyz.apac.com/dhghasgharchiacserver](http://www.xyz.apac.com/dhghasgharchiacserver)

[www.xyz.apac.com/ARCHIACSERVER](http://www.xyz.apac.com/ARCHIACSERVER)

[www.xyz.apac.com/archiacserveradjefjaehfje](http://www.xyz.apac.com/archiacserveradjefjaehfje)

[www.xyz.apac.com/archiacserver](http://www.xyz.apac.com/archiacserver))\*)\*)\*)\*)\*)

[www.xyz.apac.com/archiacPORTAL](http://www.xyz.apac.com/archiacPORTAL)

[www.xyz.apac.com/ARCCHIACPORTALjdfbjdsfjasd](http://www.xyz.apac.com/ARCCHIACPORTALjdfbjdsfjasd)

etc etc

SO my code work fine with testing and loading the APP through LB and also now looks like error handling works fine . . But i am seeing some issues with session kick out .

For example if i try to load [www.xyz.apac.com/archiacserver/admin/login](http://www.xyz.apac.com/archiacserver/admin/login) , this works fine .Page load up with username and password and work perfectly .

But if i hover to some tab after logged in and click , the session times out and ask for a re-login .

we are not doing any Load balancing , simply sending the traffic to pool and my pool is configured with ACTIVE/STANDBY nodes using priority group .\< 1 with one node being on 10 and other on 5 .

So no cookie persistence needed .

ANy suggestions ?? something wrong with my irule ?

No session kick out when i hit the node directly .

Thanks

---

<div class="post-metadata">

**Author:** ![JRahm](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/jrahm/32/3327_2.png) [@JRahm](https://community.f5.com/u/JRahm)\
**Post date:** [March 15, 2021, 4:31pm UTC](https://community.f5.com/t/http-404-error-handling-through-irule/48003/4 "2021-03-15T16:31:26Z")

</div>

If you are trying to match the path and NOT the query parameters, you’d be better off using HTTP::path, which only includes path information for that instead of HTTP::uri, which includes both path and query parameters.

For your badmatch, are you saying those are still matching, or that they are no longer matching?

finally, are you seeing any tcl errors in the logs when the sessions are being “kicked out”?

FYI, I’m going to be out on PTO for the next few days

---

<div class="post-metadata">

**Author:** ![Deepak\_Nair](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@Deepak\_Nair](https://community.f5.com/u/Deepak_Nair)\
**Post date:** [March 18, 2021, 4:04am UTC](https://community.f5.com/t/http-404-error-handling-through-irule/48003/5 "2021-03-18T04:04:50Z")

</div>

Hi Jason ,

Thanks for writing me back .

The bad matches are no longer matching .This is the irule I am using as POC :

when HTTP\_REQUEST {

set host [string tolower [HTTP::host]]

set uri [HTTP::uri]

if {$host equals “[xyz.apac.com](http://xyz.apac.com/).”} {

set context [getfield $uri “/” 2]

if {($context ne “archiacserver”) and ($context ne “archiacportal”) and ($context ne “archiac”)} {

HTTP::respond 404 content {Page Not Found Page Not Found}

} elseif { ($uri starts\_with “/archiacserver”)} {

HTTP::uri [string map {“/archiacserver” “/archiac”} [HTTP::uri]

pool PL\_BNE\_ENT\_6443

return

} elseif {$uri starts\_with “/archiac”} {

pool PL\_BNE\_ENT\_6443

}elseif {$uri starts\_with “/archiacportal”} {

pool PL\_BNE\_ENT\_archiacportal\_443

return

}

}

}

Regarding the error , I found I need to set x-forwarded for host with the HTTP profile and also at the server end they need to set a web context URL for re-write .

The issue is now fixed . Thanks !

---

<div class="post-metadata">

**Author:** ![JRahm](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/jrahm/32/3327_2.png) [@JRahm](https://community.f5.com/u/JRahm)\
**Post date:** [March 18, 2021, 6:44pm UTC](https://community.f5.com/t/http-404-error-handling-through-irule/48003/6 "2021-03-18T18:44:47Z")

</div>

good deal!
