Forum Discussion

Ben_Wilson_2412's avatar
Aug 30, 2013

Portable root domains redirects

Hi, We're looking to redirect a bunch of domains from "root" requests to "www". The rule has to allow additional subdomains through, but be portable for many root domains.

 

My idea is to split the HTTP::host on "." and count the resulting list. If it's less than 2, append "www." to HTTP::host and redirect.

 

Sound reasonable? Any better ideas?

 

Thanks! Ben

 

4 Replies

  • Try this:

    when HTTP_REQUEST {
        if { [llength [split [HTTP::host] "."]] < 3 } {
            HTTP::redirect "http://www.[HTTP::host][HTTP::uri]"
        }
    }
    
  • You can do something like this:

    when HTTP_REQUEST {
        set proto "http://"
        if { [PROFILE::exists clientssl] == 1] } {
            set proto "https://"
        }
        if { [llength [split [HTTP::host] "."]] < 3 } {
            HTTP::redirect "${proto}www.[HTTP::host][HTTP::uri]"
        }
    }
    
  • Kevin,

     

    I can't get that last iRule to paste without errors on my 11.3 box. It looks fine but complains of token errors. Thoughts?

     

    01070151:3: Rule [/Common/Redirect_Root_Domains_keep_protocol] error: line 3: [parse error: PARSE syntax 85 {syntax error in expression " [PROFILE::exists clientssl] == 1] ": extra tokens at end of expression}] [{ [PROFILE::exists clientssl] == 1] }] line 6: [parse error: PARSE syntax 173 {syntax error in expression " [llength [split [HTTP::host] "."]] < 3 ": variable references require preceding $}] [{ [llength [split [HTTP::host] "."]] < 3 }]

     

  • I had an extra square bracket in there. Try this:

    when HTTP_REQUEST {
        set proto "http://"
        if { [PROFILE::exists clientssl] == 1 } {
            set proto "https://"
        }
        if { [llength [split [HTTP::host] "."]] < 3 } {
            HTTP::redirect "${proto}www.[HTTP::host][HTTP::uri]"
        }
    }