Forum Discussion

Lucilius_223799's avatar
Lucilius_223799
Icon for Nimbostratus rankNimbostratus
Sep 24, 2015

iRule issues

I have some issues trying to create a iRule. I tried to create a redirect using

when HTTP_REQUEST {
if { ([HTTP::uri] contains "www.abc.def")}
   { 
    HTTP::redirect "http://www.abc.com"
   }
}

I'm not the handiest with iRules so I must have done something wrong but searching for the answers seems difficult.

I'm trying to make several of these with different versions similar to "" but also without the "abc.def" to make it redirect to the homepage directly.

  • Hello,

     

    Welcome to DevCentral.

     

    It seems like you're not using the correct function in your conditional IF-statement. That means, the block of code inside your conditional statement will never be processed. I've added a few comments to the code so you can get started with the most typical functions for HTTP services.

     

    when HTTP_REQUEST {
    
     What's the difference of URL, Host, URI, Path, Query?
     URL = www.abc.com/asd/asd?asd111=11
     [HTTP::host] function of this URL would return "www.abc.com"
     [HTTP::path] function of this URL would return "/asd/asd"
     [HTTP::uri] function of this URL would return "/asd/asd?asd111=11"
     [HTTP::query] function of this URL would return "asd111=11"
    
     The correct version of this code can be found below. Tip: by using HTTP::redirect you always issue a 302 redirect. I don't recommend this function at all. Instead, I recommend you get used to a slightly harder alternative, HTTP::respond which allows you to have a direct control of the redirect method and optinal HTTP headers if you wish to include them with the redirect.
    
      if { [HTTP::host] contains "www.abc.def" }{
        HTTP::respond 301 location "http://www.abc.com"
      }
    
    }
  • Hello,

     

    Welcome to DevCentral.

     

    It seems like you're not using the correct function in your conditional IF-statement. That means, the block of code inside your conditional statement will never be processed. I've added a few comments to the code so you can get started with the most typical functions for HTTP services.

     

    when HTTP_REQUEST {
    
     What's the difference of URL, Host, URI, Path, Query?
     URL = www.abc.com/asd/asd?asd111=11
     [HTTP::host] function of this URL would return "www.abc.com"
     [HTTP::path] function of this URL would return "/asd/asd"
     [HTTP::uri] function of this URL would return "/asd/asd?asd111=11"
     [HTTP::query] function of this URL would return "asd111=11"
    
     The correct version of this code can be found below. Tip: by using HTTP::redirect you always issue a 302 redirect. I don't recommend this function at all. Instead, I recommend you get used to a slightly harder alternative, HTTP::respond which allows you to have a direct control of the redirect method and optinal HTTP headers if you wish to include them with the redirect.
    
      if { [HTTP::host] contains "www.abc.def" }{
        HTTP::respond 301 location "http://www.abc.com"
      }
    
    }
    • Ed_Summers's avatar
      Ed_Summers
      Icon for Nimbostratus rankNimbostratus
      Sorry, just had to comment on how much I like this response. Answers question, provides some great information, easy to read. Tip o' the hat to you sir!
    • Lucilius_223799's avatar
      Lucilius_223799
      Icon for Nimbostratus rankNimbostratus
      Thank you very much for the reply and explanation. This helped me tons and i'll save the information for later use. I'm receiving training for this in the near future and this can help a ton as well!
  • Hello,

     

    Welcome to DevCentral.

     

    It seems like you're not using the correct function in your conditional IF-statement. That means, the block of code inside your conditional statement will never be processed. I've added a few comments to the code so you can get started with the most typical functions for HTTP services.

     

    when HTTP_REQUEST {
    
     What's the difference of URL, Host, URI, Path, Query?
     URL = www.abc.com/asd/asd?asd111=11
     [HTTP::host] function of this URL would return "www.abc.com"
     [HTTP::path] function of this URL would return "/asd/asd"
     [HTTP::uri] function of this URL would return "/asd/asd?asd111=11"
     [HTTP::query] function of this URL would return "asd111=11"
    
     The correct version of this code can be found below. Tip: by using HTTP::redirect you always issue a 302 redirect. I don't recommend this function at all. Instead, I recommend you get used to a slightly harder alternative, HTTP::respond which allows you to have a direct control of the redirect method and optinal HTTP headers if you wish to include them with the redirect.
    
      if { [HTTP::host] contains "www.abc.def" }{
        HTTP::respond 301 location "http://www.abc.com"
      }
    
    }
    • Ed_Summers's avatar
      Ed_Summers
      Icon for Nimbostratus rankNimbostratus
      Sorry, just had to comment on how much I like this response. Answers question, provides some great information, easy to read. Tip o' the hat to you sir!
    • Lucilius_223799's avatar
      Lucilius_223799
      Icon for Nimbostratus rankNimbostratus
      Thank you very much for the reply and explanation. This helped me tons and i'll save the information for later use. I'm receiving training for this in the near future and this can help a ton as well!