Forum Discussion

Robert_47833's avatar
Robert_47833
Icon for Altostratus rankAltostratus
Aug 12, 2011

how to change a string ,such as add a number after some letters

Hi,dear irule

 

 

I want to change the host name to another one

 

for example

 

www.cjj.com---->www2.cjj.com

 

cjj.com------>cjj2.com

 

*.cjj.com---->*2.cjj.com

 

 

domain [HTTP::host] 2 will return cjj.com if it is "www.cjj.com"

 

 

Thanks in advance

 

 

  • Try something like:

    
    regsub {^([^\.]*).} [HTTP::host] {${1}2.} newhost
    log local0. "[HTTP:host]  $newhost"
    

  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    While regsub would technically work, it's going to induce a lot of overhead. There are lots of other ways to go about that: scan, string map, etc.

     

     

    Also, if you're looking at maintaining a list of origin -> modified domains, then you might be better of creating a data group to store them in and just do the translation in the code, rather than building out a complex regsub or the like.

     

     

    Colin

     

  • I think something like this should work:

    switch [string tolower [HTTP::host]] {
       "cjj.com" {
          HTTP::header replace Host "cjj2.com"
       }
       default {
          HTTP::header replace Host "[getfield [HTTP::host] "cjj2.com" 1]2.cjj.com"
       }
    }
    

    Aaron
  • Hi,all friends,I use irule below to achieve this:

     

    switch -glob [string tolower [HTTP::host]] {

     

    "srwd29.com" -

     

    "www.srwd29.com" {

     

    scan [HTTP::host] {%[^.]%s} domainfirst domainleft

     

    set num 2

     

    HTTP::redirect "http://$domainfirst$num$domainleft[HTTP::uri]"

     

    }

     

    I try to add "2" after $domainfirst( HTTP::redirect "http://$domainfirst2$domainleft[HTTP::uri]"),but it doesn't work ,that is why there is a num variable

     

    Thank you for your great help

     

  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    I'm glad to see that you got it working. Bonus points for using the scan command, very cool and efficient. ;)

     

     

    Colin
  • Hi,by the way,do u know why I can't add number "2" in HTTP::redirect "http://$domainfirst2$domainleft[HTTP::uri],how to achieve this?

     

    Did I miss something?

     

  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    It's not working because it doesn't know where the first variable stops, and is trying to incorporate the 2 as part of the variable name.

    Try this instead:

    HTTP::redirect "http://${domainfirst}2${domainleft}[HTTP::uri]"

    Here's an example in tclsh:

    
    
    % set h1 www.srwd      
    www.srwd
    % set h2 9.com
    9.com
    % puts $h1$h2
    www.srwd9.com
    % puts $h12$h2
    can't read "h12": no such variable
    % puts ${h1}2${h2}
    www.srwd29.com
    % 
    

    Colin