Forum Discussion

Martin_Thomas_1's avatar
Martin_Thomas_1
Icon for Nimbostratus rankNimbostratus
Dec 04, 2013

Replace part of HTTP::host and permanently redirect

I'm looking at redirecting various hosts to a new domain.

Example 1: app.olddomain.com to app.newdomain.com Example 2: anotherapp.olddomain.com to anotherapp.newdomain.com

The problem I'm having (because I'm a newb with string manipulation) is switching that "olddomain" to the "newdomain".

I could do the following for each of the applications but it would mean a huge iRule:

when HTTP_REQUEST {
          if { [string tolower [HTTP::host]] equals "app.olddomain.com" } {
                    HTTP::respond 301 Location "https://app.newdomain.com[HTTP::uri]" 
          }
}

I was hoping to do something like this:

when HTTP_REQUEST {
          if {[HTTP::host] ends_with ".olddomain.com"} {
                    HTTP::respond 301 Location "https://--part of original host--.newdomain.com[HTTP::uri]"
           }
}

Is my idea sound? How do we accomplish the replace?

Thanks!

  • Either (this is probably more efficient);

    when HTTP_REQUEST {
        if {[HTTP::host] ends_with ".olddomain.com"} {
            HTTP::respond 301 Location "https://[getfield [HTTP::host] . 1].newdomain.com[HTTP::uri]"
           }
    }
    

    Or;-

    when HTTP_REQUEST {
        if {[HTTP::host] ends_with ".olddomain.com"} {
            HTTP::respond 301 Location "https://[string map {olddomain newdomain} [HTTP::host]][HTTP::uri]"
           }
    }
    
  • giltjr's avatar
    giltjr
    Icon for Nimbostratus rankNimbostratus

    Not 100% sure but something like the following may work:

     

    regsub -all ".olddomain.com" [HTTP::host] ".newdomain.com" newhost HTTP::respond 301 Location "https://$newhost[HTTP::uri]"

     

  • Either (this is probably more efficient);

    when HTTP_REQUEST {
        if {[HTTP::host] ends_with ".olddomain.com"} {
            HTTP::respond 301 Location "https://[getfield [HTTP::host] . 1].newdomain.com[HTTP::uri]"
           }
    }
    

    Or;-

    when HTTP_REQUEST {
        if {[HTTP::host] ends_with ".olddomain.com"} {
            HTTP::respond 301 Location "https://[string map {olddomain newdomain} [HTTP::host]][HTTP::uri]"
           }
    }
    
  • Awesome! Thanks!!

    This one fits the bill nicely and is efficient:

    when HTTP_REQUEST {
        if {[HTTP::host] ends_with ".olddomain.com"} {
            HTTP::respond 301 Location "https://[string map {olddomain newdomain} [HTTP::host]][HTTP::uri]"
           }
    }