Forum Discussion

Troy_94246's avatar
Troy_94246
Icon for Nimbostratus rankNimbostratus
Jan 27, 2014

Single I-rule with 1000+ URI Redirect

We are working on a Web site refresh and have been asked to use Big IP to redirect over 1000 pages based on URI to a different URI on same page. We have different options to do this with I-rule but I have never did an I-rule with 1000 plus redirects. Is one of the options below better than the other when there is so many redirects?

 

Hardware LTM 3600 running BIG-IP 10.2.4 Build 655.0 Hotfix HF4

 

Option 1

 

when HTTP_REQUEST { if {[HTTP::path] eq " /aboutus/ContactUs/index.html"}{ HTTP::redirect "http://[HTTP::host]/en/about/contact" } Repeat IF for every URI redirect }

 

OR Option 2

 

when HTTP_REQUEST {

 

if { [HTTP::uri] starts_with "/aboutus/ContactUs/index.html"}{

 

HTTP::redirect [string map {/aboutus/ContactUs/index.html /en/about/contact} [HTTP::uri]]

 

}

 

Repeat IF for every URI redirect }

 

Or is there a better option all together?

 

9 Replies

  • nathe's avatar
    nathe
    Icon for Cirrocumulus rankCirrocumulus

    So, my way would be to use Data groups. Not sure if this is the "best" way though. Most likely a better way than a v long irule.

    So create a Datagroup with all your redirects called redir_dg (for example):

    /aboutus/ContactUs/index.html := /en/about/contact
    etc.....
    

    And then refer to this datagroup in an irule:

    when HTTP_REQUEST {
    if { [class match [string tolower [HTTP::uri]] starts_with redir_dg] } {
        HTTP::redirect http://[HTTP::host][class match -value [string tolower [HTTP::uri]] starts_with redir_dg]
      }
    }
    

    Hope that helps. I'm sure better iRules than I will think of a better way but it might get you started.

    N

  • For that many redirects, I'd HIGHLY recommend a data group list to map to-from information, and then a much smaller iRule.

    Example iRule:

    when HTTP_REQUEST {
        if { [class match [string tolower [HTTP::uri]] starts_with my_uri_dg] } {
            HTTP::redirect "http://[HTTP::host][class match -value [string tolower [HTTP::uri]] starts_with my_uri_dg]"
        }
    }
    

    Example (string-based) data group list:

    /aboutus/contactus/index.html := /en/about/contact
    /aboutyou/contactme/index.html := /en/about/contactme
    /aboutfred/contactfred/index.html := /en/about/contactfred
    

    Also notice that the left side is all lowercase, because you're evaluating a [string tolower ] match in the iRule. In this way you can manage a 1000 line data group list instead of a much larger and much more complicated iRule.

  • iControl is another option to build data groups. I've used this extensively for adding records into data groups by iterating through a CSV file containing /olduri/ and /new/uri key/value pairs...

    function Add-DataGroupValues()
    {
        param([string]$dataGroupName,[string[]]$dataGroupMember,[string]$dataGroupValue)
        $stringClass = New-Object -TypeName iControl.LocalLBClassStringClass
        $stringClass.name = $dataGroupName
        $stringClass.members = ((,$dataGroupMember))
        (Get-F5.iControl).LocalLBClass.add_string_class_member((,$stringClass))
    
        $stringValueClass = New-Object -TypeName iControl.LocalLBClassStringClass
        $stringValueClass.name = $dataGroupName
        $stringValueClass.members = ((,$dataGroupMember))
        $values = (Get-F5.iControl).LocalLBClass.set_string_class_member_data_value($stringValueClass,$dataGroupValue)
        }
    
     Get the CSV info
    $records = Get-CSVInfo
    
     Add to data group
    foreach($record in $records)
    {
        $dataGroupMember = $record.Name
        $dataGroupValue = $record.Value
        Add-DataGroupValues $dataGroupName $dataGroupMember $dataGroupValue
    }
    (Get-F5.iControl).SystemConfigSync.save_configuration("bigip.conf","SAVE_FULL")
    
  • We implemented and it doesn't redirect correctly. Below is I-rule and a sample of Data Group. Would this type of redirect have issue on 10.2.4 Build 655.0 Hotfix HF4? I see I-rule being executed on statistices. Is there any other ways to troubleshoot this?

    I-rule

    when HTTP_REQUEST {

    if { [class match [string tolower [HTTP::uri]] starts_with redir_dg] } {

    HTTP::redirect http://[HTTP::host][class match -value [string tolower [HTTP::uri]] 
    starts_with redir_dg]
    

    } }

    Data Group - redir_dg

    /aboutus/ContactUs/index.html := /en/about/contact

    /aboutus/news/index.html := /en/about/news

    /aboutus/ := /en/about-us

    /aboutus/place/index.html := /en/about-us

    • Arie's avatar
      Arie
      Icon for Altostratus rankAltostratus
      Since you're converting [HTTP::host] to lower case, you should convert all the keys (left column) to lower case also.
    • Arie's avatar
      Arie
      Icon for Altostratus rankAltostratus
      Observations: 1. "/index.html" = "/", so you'll want to add both to the data group. 2. Using [HTTP::path] is a bit leaner than using [HTTP::uri]. 3. HTTP::redirect generates a 302, which is not cached. It would be better to use HTTP::respond 301 Location http://[HTTP::host][class match -value [string tolower [HTTP::uri]] starts_with redir_dg] 4. Consider using quotation marks around the Location, just in case someone uses spaces or other troublesome characters in the Location. 5. One of the problems with Data Groups is that (depending on how they're updated), updating the DG temporarily makes it unavailable and/or will contain no data or merely a subset of the data. Although the duration of this error condition is brief, it can be problematic. 6. If any other redirects or responses are generated on the connection you'll get an error ("multiple responses"). Code accordingly.
  • Arie's avatar
    Arie
    Icon for Altostratus rankAltostratus

    When you say "it doesn't redirect correctly", what do you mean exactly? Does it not redirect at all, or are there unintended actions taken? Any errors in the logs?

     

  • I found my issue. Some worked and others did not. The ones that didn't work I had uppercase letters in my Data Group. I tried to copy and past a list i recieved from the web team to save time but it ended up bitting me. Thank you for the quick repsonce! it ended up being a case of upper case Datagroup when I-rule specifically changes to lower case!