Forum Discussion

Simon_Wright_85's avatar
Simon_Wright_85
Icon for Nimbostratus rankNimbostratus
Feb 27, 2007

Subdomain iRule question

Hi all

 

 

I am just starting out creating iRules for our Big-ip boxes and i have no idea where to start with this one.

 

 

What i am trying to do is to convert the subdomain of a url into a directory name that it passed through to the webserver like this.

 

 

subdomain.domain.com -> F5 box -> webserver/subdomain

 

 

I cannot find how to extract the subdomain and use it as a variable to generate the request that is sent to the webserver.

 

 

If anyone knows how this could be done or if there are any tutorials that they could point me towards that would be most appreciated.

 

 

many thanks

 

 

Simon
  • You can get or set the HTTP host using HTTP::host. The URI can be gotten or set using HTTP::uri.

    You can use getfield to pick out the parts of the host and then prepend them to the URI. You may want to add additional logic to handle the possibility that the host might not contain a string you want to prepend to the URI. For example, if a client requests http://www.example.com/someuri, you may not want to redirect them to http://example.com/www/someuri.

    Here's an example to get you started:

    
    when RULE_INIT {
        Set the value for the domain with a leading period
          We'll check the host header value for any string the comes before this string 
          and prepend it to the URI
       set ::domain ".example.com"
    }
    when HTTP_REQUEST {
       if { [string tolower [HTTP::host]] ends with $::domain }{
           Get any part of the host that comes before the domain.
             Split the URI using the domain string.  Grab the first string that comes before the domain
          set subdomain [getfield [HTTP::host] $::domain 1]
           If subdomain isn't null, prepend it to the URI 
             You could also add additional checks here like subdomain doesn't equal       "www"
          if { not ($subdomain == "") }{
     HTTP::uri [concat "/$subdomain[HTTP::uri]"]
              Set the HTTP host to the domain
             HTTP::host [string range $::domain 1 end]
          }
       }
    }

    Edit: I forgot to change the host header to the domain only

    Aaron