Forum Discussion

KimiLi_147173's avatar
KimiLi_147173
Icon for Nimbostratus rankNimbostratus
Nov 27, 2017

Want to rewrite part of host

Hi guys, I'm still new to iRules, I wonder that if it is possible that an iRule can rewrite part of the host instead of all of it.

 

I mean like, if a client request a host like "123.aaa.com/888/", he will be redirected to the "123.bbb.com/888/", I just want to rewrite the "aaa.com" part, which is when requests to host like "xxx.aaa.com/xxx/" I'd like it to be redirected to "xxx.bbb.com/xxx/", is this possible?

 

Thanks guys.

 

  • If string position is known then you can use

    getfield
    function to grep value like below.

    set hostname [getfield [HTTP::host] "." 1]   If the URL is xxx.aaa.bbb.com, then getfield command will grep value of 1st position xxx
    set hostname1 [getfield [HTTP::host] "." 2]   If the URL is xxx.aaa.bbb.com, then getfield command will grep value of 2nd position aaa
    

    Refer below link. Hope it will be helpful.

  • Do you want to do a HTTP redirect or just change the Host Header before you forward to a pool member?

     

  • Want to redirect to another website.

     

    I've already got that how to write an irule to rewrite the whole host a client requests to get, but how to rewrite part of the host, and make clients go to it, that's my issue, cause xxx.aaa.com, by the string xxx I mean it's a dynamic one.

     

  • can you test this?

    when HTTP_REQUEST {
    if {[string tolower [HTTP::host]] eq "xxx.aaa.com" }{
        HTTP::redirect "http://xxx.bbb.com[HTTP::uri]"
    } 
    }
    
  • That's not my purpose, by xxx I mean a dynamic string, it could be xxx, 123, aaa or whatever. Thanks anyway.

     

  • If string position is known then you can use

    getfield
    function to grep value like below.

    set hostname [getfield [HTTP::host] "." 1]   If the URL is xxx.aaa.bbb.com, then getfield command will grep value of 1st position xxx
    set hostname1 [getfield [HTTP::host] "." 2]   If the URL is xxx.aaa.bbb.com, then getfield command will grep value of 2nd position aaa
    

    Refer below link. Hope it will be helpful.

    • KimiLi_147173's avatar
      KimiLi_147173
      Icon for Nimbostratus rankNimbostratus

      Hi f5_rock,

      YOU'RE THE MAN!

      I believe this is what I want, according to your suggestion I've got an irule like this:

      when HTTP_REQUEST {
       Check if host contains "aaa.com"
      if {[HTTP::host] contains "aaa.com"}{
           Get the xxx from host xxx.aaa.com
          HTTP::redirect "https://[getfield [HTTP::host] "." 1].bbb.com[HTTP::uri]"
         }
      }
      

      And it works! I'll keep on doing further tests to figure out if there's still room to make it better.

      Thanks again!