# irule to replace any .asp to be .aspx in uri

**URL:** <https://community.f5.com/t/irule-to-replace-any-asp-to-be-aspx-in-uri/1053>\
**Category:** Technical Forums\
**Tags:** irules, application-delivery\
**Created:** [October 17, 2016, 8:55pm UTC](https://community.f5.com/t/irule-to-replace-any-asp-to-be-aspx-in-uri/1053 "2016-10-17T20:55:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcus\_10406](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Marcus\_10406](https://community.f5.com/u/Marcus_10406)\
**Post date:** [October 17, 2016, 8:55pm UTC](https://community.f5.com/t/irule-to-replace-any-asp-to-be-aspx-in-uri/1053/1 "2016-10-17T20:55:29Z")

</div>

hi there,

our APPDEV team requesting a uri replacement to be done on my LTM to realize following:

for any incoming URL, replace .asp by .aspx. for example: [123.abc.com/xxx.asp](http://123.abc.com/xxx.asp) will be replaced as [www.abc.com/xxx.aspx](http://www.abc.com/xxx.aspx) [123.abc.com/xxx.asp?xxxxxxxx](http://123.abc.com/xxx.asp?xxxxxxxx) to be replaced as [www.abc.com/xxx.aspx?xxxxxxxx](http://www.abc.com/xxx.aspx?xxxxxxxx)

to meet this requirement, I need to check the following charactor of ‘.asp’ is not ‘x’. anyone has experience on how to irule it?

here is the one I did:

```tcl
when HTTP_REQUEST {
    switch -glob [HTTP::host][HTTP::uri] {
        "wm.456.abc.com/*" {
             HTTP::redirect "https://teams.abc.com[HTTP::uri]"
        }
        "123.abc.com/*" { 
            if { [HTTP::uri] contains ".aspx"} {
                HTTP::redirect "https://www.abc.com[HTTP::uri]"
            } elseif {[HTTP::uri] contains ".asp"} {
                set uri [string map {".asp" ".aspx"}[HTTP::uri]]
                HTTP::redirect "https://www.abc.com$uri" 
            } else {
                HTTP::redirect "https://knowledge.cibc.com[HTTP::uri]" 
            }
        } 
        default { 
            pool /PROD/po_abc
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![VernonWells](https://avatars.discourse-cdn.com/v4/letter/v/dec6dc/32.png) [@VernonWells](https://community.f5.com/u/VernonWells)\
**Post date:** [October 17, 2016, 9:21pm UTC](https://community.f5.com/t/irule-to-replace-any-asp-to-be-aspx-in-uri/1053/2 "2016-10-17T21:21:06Z")

</div>

I apologize, but beyond the .asp to .aspx part, it’s not entirely clear how else you want to redirect. However, assuming, I’m assuming you want the following:

```markup
if host == 123.abc.com, then
  if Request Target Path ends_with .asp or .aspx, then 
     redirect to www.abc.com[path][query-params], with .asp changed to .aspx
  else redirect to knowledge.cibc.com[path][query-params]
else
  do not redirect

```

Adjust the following as needed for your actual requirements:

```markup
when HTTP_REQUEST {
    if { [string tolower [HTTP::host]] eq "123.abc.com" } {
        switch -glob "[HTTP::path]" {
            "*.asp" {
                HTTP::respond 301 Location "https://www.abc.com[HTTP::path]x[HTTP::query]"
            }

            "*.aspx" {
                HTTP::respond 301 Location "https://www.abc.com[HTTP::uri]"
            }

            default {
                HTTP::respond 301 Location "https://knowledge.cibc.com[HTTP::uri]"
            }
        }
    }
}

```

Notice that I cut your **default** branch, which made a **pool** call. If the pool assigned the Virtual Server is already _/PROD/po\_abc_ there is no need to explicitly identify the pool here. If the rule doesn’t change the pool selection, then the pool assigned to the Virtual Server will automatically be used.

---

<div class="post-metadata">

**Author:** ![Marcus\_10406](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Marcus\_10406](https://community.f5.com/u/Marcus_10406)\
**Post date:** [October 18, 2016, 1:25pm UTC](https://community.f5.com/t/irule-to-replace-any-asp-to-be-aspx-in-uri/1053/3 "2016-10-18T13:25:46Z")

</div>

hi Vernon, thanks for your reply. however when you are doing switch command, you cannot use “_.asp" and "_.aspx” as criteria to determine the action because _.asp_ is same as part of _aspx_, so if some incoming traffic with http header uri including /xxx.aspx, it will be replaced as /xxxx.aspxx by your irule. that’s also the problem I had before.

---

<div class="post-metadata">

**Author:** ![VernonWells](https://avatars.discourse-cdn.com/v4/letter/v/dec6dc/32.png) [@VernonWells](https://community.f5.com/u/VernonWells)\
**Post date:** [October 18, 2016, 3:23pm UTC](https://community.f5.com/t/irule-to-replace-any-asp-to-be-aspx-in-uri/1053/4 "2016-10-18T15:23:58Z")

</div>

**switch** matching is not sub-string matching. Indeed, by default, it is absolute matching. Thus, if I did something like this:

```markup
switch $x {
    "fo" { ... }
    "bar" { ... }
    "foo" { ... }
}

```

If **$x** is the value “foot”, none of the cases would match. If, instead, **$x** is “foo”, only the last case would match.

The **-glob** flag to **switch** changes the matching style to “glob”. With this style, any instance of an asterisk (\*) matches zero or more characters, a question-mark (?) matches exactly (any) one character, and characters in square-braces ([…]) form a character class (e.g., “[adgk]” would match a single character if and only if that character is a, d, g or k).

Putting this together, in this example:

```markup
switch $x {
    "foo" { ... }
    "*foo" { ... }
    "foo*" { ... }
    "*foo*" { ... }
}

```

Case 1 will match if and only if **$x** is exactly the string “foo”. Case 2 will match if **$x** ends with the string “foo”. Case 3 will match if **$x** starts with the string “foo”. Case 4 will match if **$x** contains the string “foo”.

In my code:

```markup
switch -glob "[HTTP::path]" {
   "*.asp" {...}
   "*.aspx" {...}

```

the first case will match if **HTTP::path** ends with the string “.asp”. The second case will match if **HTTP::path** ends with the string “.aspx”.

---

<div class="post-metadata">

**Author:** ![immu](https://avatars.discourse-cdn.com/v4/letter/i/e480ec/32.png) [@immu](https://community.f5.com/u/immu)\
**Post date:** [June 7, 2018, 2:56pm UTC](https://community.f5.com/t/irule-to-replace-any-asp-to-be-aspx-in-uri/1053/5 "2018-06-07T14:56:28Z")

</div>

Hi Team,

I tried applying the above rule but its giving me error related to brackets and if statement

Can some one please send me the exact rule for asp to \*.aspx redirect.

Thanks in anticipation.

---

<div class="post-metadata">

**Author:** ![VernonWells](https://avatars.discourse-cdn.com/v4/letter/v/dec6dc/32.png) [@VernonWells](https://community.f5.com/u/VernonWells)\
**Post date:** [June 8, 2018, 11:15am UTC](https://community.f5.com/t/irule-to-replace-any-asp-to-be-aspx-in-uri/1053/6 "2018-06-08T11:15:37Z")

</div>

The original was missing a closing brace for the **switch**.&nbsp;I have updated my original post to include the missing brace, and just tested it. It parses correctly now.
