Forum Discussion

sandy16's avatar
sandy16
Icon for Altostratus rankAltostratus
Dec 03, 2014

Irule help for host header redirection

Hi experts, I need to write an irule which will redirect some different host headers/urls to one single-different url. After doing some digging around on devcentral i found this one should do the job -

 

when HTTP_REQUEST { if {[HTTP::host] equals "www.example.com" } { HTTP::redirect "https://mywebsite.com" } }

 

Let`s say I also need to redirect two more host headers like "abccorp.com" and "xyzmedia.ca" to the same "https://mywebsite.com" , how can i redo my above irule? Should it be with multiple "if" statements?

 

5 Replies

  • if it's a list of hostnames that isn't too large and won't be changing too often, i would just use some or's:

    when HTTP_REQUEST {
     if {([string tolower [HTTP::host]] equals "www.example.com") or ([string tolower [HTTP::host]] equals "abccorp.com" or [string tolower [HTTP::host]] equals "xyzmedia.ca")} {
      HTTP::redirect "https://mywebsite.com"
     }
    }
    

    if you want the irule to be more flexible and extensible in regards to adding hostnames, you should use a data group. create data-group (reference in irule below as hostname-data-group) with a list of the hostnames you want redirected, and use an irule similar to:

    when HTTP_REQUEST {
     if { class match [string tolower [HTTP::host]] equals hostname-data-group } {
      HTTP::redirect "https://mywebsite.com"
     }
    }
    
  • shaggy's avatar
    shaggy
    Icon for Nimbostratus rankNimbostratus

    if it's a list of hostnames that isn't too large and won't be changing too often, i would just use some or's:

    when HTTP_REQUEST {
     if {([string tolower [HTTP::host]] equals "www.example.com") or ([string tolower [HTTP::host]] equals "abccorp.com" or [string tolower [HTTP::host]] equals "xyzmedia.ca")} {
      HTTP::redirect "https://mywebsite.com"
     }
    }
    

    if you want the irule to be more flexible and extensible in regards to adding hostnames, you should use a data group. create data-group (reference in irule below as hostname-data-group) with a list of the hostnames you want redirected, and use an irule similar to:

    when HTTP_REQUEST {
     if { class match [string tolower [HTTP::host]] equals hostname-data-group } {
      HTTP::redirect "https://mywebsite.com"
     }
    }
    
  • Here's another options as well, really just comes down to how much data you got and how you want to manage it. Personally I like the second options Shaggy suggested.

    when HTTP_REQUEST {
        set host [string tolower [HTTP::host]]
        switch $host {
            "site1.com" -
            "site2.com" -
            "www.site3.com" {
                HTTP::redirect "https://mywebsite.com"
            }
        }
    }