Forum Discussion

rwagner1's avatar
rwagner1
Icon for Nimbostratus rankNimbostratus
Sep 25, 2018

Multi site redirect using host headers

We have a VIP setup which uses host headers. I would like an iRule to redirect the main domain to the sub domain for all the sites using the same VIP, and still send it to the sub domain pool.

 

Example: abc.com gets redirected to and gets sent to the pool named . mno.com gets redirected to and gets sent to the pool named xyz.com gets redirected to and gets sent to the pool named

 

We are trying to not have to setup 2 pools for every domain. Example:

 

Pool 1 Pool 2 abc.com Pool 3 Pool 4 mno.com Pool 5 Pool 6 xyz.com

 

We have 2 iRule assigned to that VIP.

 

1)The first iRule looks at the HTTP request and sends it to the pool with the same name.

 

when HTTP_REQUEST { pool [HTTP::host] }

 

2)The second iRule looks at the pool to make sure the members are up, if they are down it sends our standard App-Offline page.

 

when HTTP_REQUEST { set Vuri [ string tolower [HTTP::uri]] set Vheader [string tolower [HTTP::host]] set Poolname "$Vheader" set SiteName "XXXX.MAINTENANCEPAGE.org" set Irulename "Irule_$SiteName" set SiteRedirect "https://$SiteName" set Poolmember [active_members $Poolname ] set Poolmemberlist [ active_members -list $Poolname] if {$Poolmember < 1} then { log local0.alert "ALERT-TEAM Pool $Poolname is down. This mean $SiteName website is down. IRULE=$Irulename"; HTTP::respond 200 content [ifile get App_Offline] } pool $Poolname }

 

5 Replies

  • Hi,

    you can use this irule without redirect but just rewrite host header:

     

    when HTTP_REQUEST {
      switch -glob [string tolower [HTTP::host]] {
        "abc.com" { 
            pool "www.abc.com"
            HTTP::header replace Host "www.abc.com"
        }
        "abc.com" { 
            pool "www.mno.com"
            HTTP::header replace Host "www.mno.com"
        }
        "xyz.com" { 
            pool "www.xyz.com"
            HTTP::header replace Host "www.xyz.com"
        }
        default { 
             do nothing
        }
      }
    }
    

     

    if you want use redirect just replace:

    HTTP::header replace Host "www.abc.com"

    by

    HTTP::redirect "https://www.abc.com[HTTP::uri]"

    regards,

  • From what I see in that iRule is that every time I add a site I would have to edit the irule. Is there a way to make it generic? If anything.com comes in without the sub-domain add www.

     

    Example. abc.com comes in send but if test.abc.com comes in sent it as is test.abc.com.

     

  • Hi,

    If you want a generic Irule, you can use something like that:

     

    when HTTP_REQUEST {
    
    set host [string tolower [HTTP::host]
    
    if {!($host starts_with "www.") and ($host ends_with "com")} {
        set host2 "www.[string tolower [HTTP::host]"
        HTTP::header replace Host $host2
    }
    }
    

     

    if you want use redirect just replace:

    HTTP::header replace Host $host2

    by

    HTTP::redirect "https://$host2[HTTP::uri]"

  • Hi Rwagner,

    security wise its always a good idea to use a white-list of acceptable values before passing user provided information to subsequent iRule commands. Depending on you remaining configuration you original iRule may allow the user to select a pool which was not intended...

    You may check to the iRule below. It will white-list the individual [HTTP::host] values and assign a pool or perform a 301 - Permanent Redirect as needed and last but not least send a 503 - Bad Gateway response for every request with an unknown [HTTP::host] value.

    After assigning the [pool], the iRule checks if the currently selected pool object has [active_members] available . If not the iRule would respond with your Maintenance-Page using a 502 - Service Unavailable response code to not hurt your Google search results and SEO ratings...

     

    when HTTP_REQUEST { 
        switch -exact -- [string tolower [HTTP::host] {
            "www.abc.com" {
                pool "www.abc.com"
            }
            "www.xyz.com" {
                pool "www.xyz.com"
            }
            "abc.com" {
                HTTP::respond 301 "Location" "//www.abc.com[HTTP::uri]"
                return
            }
            "xyz.com" {
                HTTP::respond 301 "Location" "//www.abc.com[HTTP::uri]"
                return
            }
            default {
                HTTP::respond 503 content "Bad Gateway" "Content-Type" "text/plain" 
                return
            }       
        }
        if { [active_members [LB::server pool]] < 1 } then {
            log local0.alert "ALERT-TEAM Pool \"[LB::server pool]\" is down. This mean \"[HTTP::host]\" website is down."; 
            HTTP::respond 502 content [ifile get App_Offline] 
        }
    }
    

     

    Cheers, Kai