Forum Discussion

BuccaneerDave_2's avatar
BuccaneerDave_2
Icon for Nimbostratus rankNimbostratus
Feb 20, 2015

trouble getting redirect irule to work

Hello,

I need to have traffic from:

  • hostA.xyzcompanyresources.com/*

redirect to:

  • stageamericas.internal.xyzcompanyonline.com/pages/legacyredirection.aspx?legacyurl=hostA.xyzcompanyresources.com/*

I have been trying this:

when RULE_INIT {
  set ::org "O=xyzcompany"
  log "desired host.xyzcompanyresources.com_irule initialized"
}

when HTTP_REQUEST {
  set destination_path "pages/legacyredirection.aspx?legacyurl="

  set destination_host "stageamericas.internal.xyzcompanyonline.com/"

   Check if requested host starts  with desired host.xyzcompanyresources.com

  if {([string tolower  [HTTP::host]] starts_with $::communities_host_list)} {
    set content [string tolower [HTTP::uri]]    
    set redirect_to "${destination_host}${destination_path}[HTTP::host]${redirect_uri}"
    if {$redirect_uri ne ""}{
      HTTP::redirect https://$redirect_to
    }
  }
}

it worked - came in the next day and they say it doesn't work anymore

my data group list is called communities_host_list

it contains : hostA.xyzcompanyresources.com

ltm is v10.2.4

thanks

3 Replies

  • Hi BuccaneerDave. I'd start a little smaller. Some thoughts

     

    1. I don't know how that would have worked because redirect_uri variable isn't even defined (did you mean content?)
    2. In the age of CMP (clustered multi-processing) you should avoid global variables at all costs as it pins your virtual server traffic to one tmm instance.
    3. Unless you are supporting scripts/custom non-compliant tools as clients, you don't need to set the host to lower case, browsers will do this automatically per standard. Also, note that the HTTP::host command returns only the fqdn unless your virtual is on a non-standard 80/443 port, in which case it will return fqdn:
    4. Variables can be helpful when you are getting started to provide clarity, but given the simplicity of this effort, you really shouldn't use them.
    5. If you are going to have many hosts to re-map, a datagroup is a good idea, but you'll need to use the class command to extract the data. Also, I'd recommend setting the old host as the key and the new host as the value in that datagroup so you can keep all the data separate from the logic of the iRule.
    6. I've made some assumptions of what I think you are attempting here. Given the specifics of your example, the code below should work for you.

    hope this helps sir.

     

    when HTTP_REQUEST { 
      if { [HTTP::host] equals "hostA.xyzcompanyresources.com" } { 
        HTTP::redirect https://stageamericas.internal.xyzcompanyonline.com/pages/legacyredirection.aspx?legacyurl=[HTTP::host][HTTP::uri]
      }
    }

    one more thing...for this url

     

    https://devcentral.f5.com/questions/trouble-getting-redirect-irule-to-work?legacyurl=test

     

    • HTTP::host : devcentral.f5.com
    • HTTP::path : /questions/trouble-getting-redirect-irule-to-work
    • HTTP::uri : /questions/trouble-getting-redirect-irule-to-work?legacyurl=test
    • HTTP::query : legacyurl=test
  • Thank you, you suggestion works, However, I am having trouble getting it to work with my datagroup.

     

    for this line:

     

    if { [HTTP::host] equals "hostA.xyzcompanyresources.com" }

     

    I tried changing "hostA.zyxcompanyresources.com" to $::communities_host_list but it doesn't work.

     

  • I tried changing "hostA.zyxcompanyresources.com" to $::communities_host_list but it doesn't work.

    $:: prefix is no longer needed since 9.4.4.

    9.4.0 - 9.4.3, class reference not compatible as of 9.4.4, "::" and "$::" prefixes are no longer required to reference classes using findclass or matchclass. Classes are static and are therefore CMP compatible. There is no need to treat them as global objects.
    

    CMP Compatibility

    https://devcentral.f5.com/wiki/iRules.cmpcompatibility.ashx

    can you try this?

    if { [class match -- [HTTP::host] equals communities_host_list] } {
      ...
    }
    

    class

    https://devcentral.f5.com/wiki/iRules.class.ashx