Forum Discussion

Marcus_10406's avatar
Marcus_10406
Icon for Nimbostratus rankNimbostratus
Oct 17, 2016

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

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 will be replaced as www.abc.com/xxx.aspx 123.abc.com/xxx.asp?xxxxxxxx to be replaced as 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:

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
        }
    }
}

 

5 Replies

  • 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:

     

    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:

     

    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.

    • Marcus_10406's avatar
      Marcus_10406
      Icon for Nimbostratus rankNimbostratus

      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.

       

    • immu's avatar
      immu
      Icon for Altostratus rankAltostratus

      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.

       

    • VernonWells's avatar
      VernonWells
      Icon for Employee rankEmployee

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

       

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

     

    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:

     

    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:

     

    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".