Forum Discussion

Gill_32697's avatar
Gill_32697
Icon for Nimbostratus rankNimbostratus
Feb 10, 2014

uri redirects

So i need to redirect to website based on a uri. I have about 100 redirect and I my need to add, remove or edit from time to time. Below is what I have but is giving me an error when I save in editor. Id like to use one line entries for each if possible, this way all i would need to do is add a new line. Below is a sample....can you check why im getting error or not working. ! ! when HTTP_REQUEST { {[HTTP::uri] equals {/Bob}} {HTTP::uri {https://weblet.mysites.com/mortgage/default.aspx?ClientID=11039} {[HTTP::uri] equals {/Sue}} {HTTP::uri {https://weblet.mysites.com/mortgage/default.aspx?ClientID=11068} } }

 

7 Replies

  • For that many records, I'd use a data group> This way you never have to touch the iRule.

    1. Create a string-based data group. Example (my_uri_dg)

      "/bob" := "https://weblet.mysites.com/mortgage/default.aspx?ClientID=11039"
      "/sue" := "https://weblet.mysites.com/mortgage/default.aspx?ClientID=11039"
      
    2. Create this iRule (modify as required):

      when HTTP_REQUEST {
          if { [class match [string tolower [HTTP::uri]] equals my_uri_dg] } {
              HTTP::redirect [class match -value [string tolower [HTTP::uri]] equals my_uri_dg]
          }
      }
      

    This will redirect the client to the correct URL based on the request URI.

  • Im not sure how to use data strings. I need to have this done in the next few hours. Cant I just add them into an irule as one liners?

     

  • I created it, i like it. But its not redirecting. Im seeing hits to the iRule but never redirects. I checked the name if the irule and name of data string all the same. So just to clearify.. browser url will be https://MyMortgage.com/Bob or https://MyMortgage.com/Sue .... ect. But the browser will redirected based on the uri(/Bob or /Sue) to https://weblet.mysite.com/mortgage/default.aspx?ClientID=11039 or https://weblet.swbc.com/mortgage/default.aspx?ClientID=11038 for Sue and so on...almost got it working just seems the Data Group is not matching. Question should the sting be "/Bob" or /Bob ? ive tried both, neither seems to work.

     

  • Notice that I'm doing a [string tolower ] comparison. Your data group strings should then all be lowercase (ex. /bob, /sue, /fred, etc.). I modified the iRule a little to use a "starts_with" conditional instead of "equals".

    when HTTP_REQUEST {
        if { [class match [string tolower [HTTP::uri]] starts_with my_uri_dg] } {
            HTTP::redirect [class match -value [string tolower [HTTP::uri]] starts_with my_uri_dg]
        }
    }
    
  • Its working now, that was the problem. I was adding the data strings with a capital first letter for the name. Thanks Kevin.

     

  • hi i have the same issue, i tried the same but when user types lower case redirection works but when users types uppers it is not working..

    eg user come /bseu/finance it should redirect to /sites/finance this is working..now if user comes with /bseu/FINANCE it is not redirecting to sites.

    if { [class match [string tolower [HTTP::path]] starts_with test] } {
        set origin [class match -name [string tolower [HTTP::path]] starts_with test]
        set new [class match -value [string tolower [HTTP::path]] starts_with test]
        HTTP::uri [string map "$origin $new" [HTTP::uri]]
            log local0. "New URI = $new" }
    
  • Arie's avatar
    Arie
    Icon for Altostratus rankAltostratus

    @Shakti

    When you do the replacement at the end you're trying to match $origin in the original URI, which would contain the upper case string if that's what the user requested.

    Try this:

    HTTP::uri [string map "$origin $new" [string tolower [HTTP::uri]]]

    However, if the web server (or the application) is case-sensitive this may cause other problems.