Forum Discussion

Will_Caetano_97's avatar
Will_Caetano_97
Icon for Nimbostratus rankNimbostratus
Sep 07, 2016

HELP - Wildcard URI Redirect

I'm new to iRules. And I'm trying to create what should be a simple iRule. I would like to redirect traffic from

 

http://www.domain1.com/products/new/*

 

to

 

http://www.domain1.com/custom/new-items/*

 

So far, here's what I have

 

when HTTP_REQUEST { if {[string tolower [HTTP::uri]] starts_with "products/new/"} {HTTP::respond 301 Location "http://www.domain1.com/custom/new-items/*"}

 

thanks for the help. I'm sure it's just something simple that I'm missing.

 

11 Replies

  • HTTP::uri ALWAYS starts with / so your code should read:

     

    if {[string tolower [HTTP::uri]] starts_with "/products/new/"
    • ekaleido_26616's avatar
      ekaleido_26616
      Icon for Cirrocumulus rankCirrocumulus

      Also, if you just want to replicate whatever comes after new/ you are not going to get away with just doing a redirect. You're going to have to capture everything after new/ to a variable to insert in the redirect.

       

    • Will_Caetano_97's avatar
      Will_Caetano_97
      Icon for Nimbostratus rankNimbostratus

      Thank you for the feedback. Would you have any example on how to capture everything after new/. Sorry I'm really new to iRules.

       

    • ekaleido_26616's avatar
      ekaleido_26616
      Icon for Cirrocumulus rankCirrocumulus

      I was afraid you'd ask. My TCL-fu sucks but this might be one way... 🙂

      set firstUri [HTTP::uri]
      set newUri [split $firstUri "/"]
      foreach field $newUri {
        lassign $newUri field1 field2 field3 fieldHOWEVERMANYFIELDSYOUHAVE
      }
      

      Then in the redirect instead of /* it would be /$field1/$field2 etc, etc.

      Like I said, my TCL-fu stinks, but someone will probably provide a more efficient answer.

  • HTTP::uri ALWAYS starts with / so your code should read:

     

    if {[string tolower [HTTP::uri]] starts_with "/products/new/"
    • ekaleido's avatar
      ekaleido
      Icon for Cirrus rankCirrus

      Also, if you just want to replicate whatever comes after new/ you are not going to get away with just doing a redirect. You're going to have to capture everything after new/ to a variable to insert in the redirect.

       

    • Will_Caetano_97's avatar
      Will_Caetano_97
      Icon for Nimbostratus rankNimbostratus

      Thank you for the feedback. Would you have any example on how to capture everything after new/. Sorry I'm really new to iRules.

       

    • ekaleido's avatar
      ekaleido
      Icon for Cirrus rankCirrus

      I was afraid you'd ask. My TCL-fu sucks but this might be one way... 🙂

      set firstUri [HTTP::uri]
      set newUri [split $firstUri "/"]
      foreach field $newUri {
        lassign $newUri field1 field2 field3 fieldHOWEVERMANYFIELDSYOUHAVE
      }
      

      Then in the redirect instead of /* it would be /$field1/$field2 etc, etc.

      Like I said, my TCL-fu stinks, but someone will probably provide a more efficient answer.

  • Hi,

    you an try this irule :

    when HTTP_REQUEST {
        if {[string tolower [HTTP::uri]] starts_with "/products/new/"} {
            set newuri [string map "/products/new/  /custom/new-items/" [HTTP::uri]]
            HTTP::respond 301 Location "http://www.domain1.com$newuri"
    }