Forum Discussion

Chris_Robert_10's avatar
Chris_Robert_10
Icon for Nimbostratus rankNimbostratus
Sep 15, 2006

URL rewrite plus

I currently have this iRule running:

 

 

when HTTP_REQUEST {

 

if { [HTTP::uri] equals "/www.mysite.com/" } {

 

HTTP::uri "/www.mysite.com/index.htm/"

 

}

 

}

 

 

 

They now want me to force the addition of www if the enduser does not enter it. How can I do this without getting www.www.mysite.com/index.htm/?

 

 

Sorry for a noob question, but I rarely am in this system.
  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    URL's take the form of:

     

    scheme://host/uri

     

     

    For this URL: http://www.mysite.com/index.htm

     

     

    [HTTP::host] will return "www.mysite.com"

     

    and

     

    [HTTP::uri] will return "/index.htm"

     

     

    If you just want to change the Host header to www.mysite.com and explicitly include the page name in the URI, you can use this code:
    when HTTP_REQUEST {
      if {[string tolower [HTTP::host] equals "mysite.com" } {
        HTTP::header replace Host "www.mysite.com"
        HTTP::uri "/index.htm"
      }
    }
    HTH

     

    /deb

     

  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    Actually, this makes more sense for the URI replacement:
    when HTTP_REQUEST {
      if {[string tolower [HTTP::host] equals "mysite.com" } {
        HTTP::header replace Host "www.mysite.com"
      } 
      if {[string length [HTTP::uri]] <= 1 } {
        HTTP::uri "/index.htm"
      } 
    }
  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    I think he may actually want a redirect, so the users browser actually get's updated to include the www.
  • I am stumbling with the same issue. I need to add "www" only if the person did not enter it as the host.

    I am starting with the following iRule.

    
    when HTTP_REQUEST {
    if { [HTTP::uri] equals "/" } {
          HTTP::redirect https://[HTTP::host]/login.do
    } else {
    HTTP::redirect https://[HTTP::host][HTTP::uri]
    } 
    }

    I am using the above rule as a generic redirect across 20 or so domain names. I only need to place the www in front if the HTTP::host only contains a root domain name.

    For example I need the following to occur:

    http://domain.com --> https://www.domain.com

    http://host1.domain.com --> https://host1.domain.com

    http://host5.site1.domain.com --> https://host5.site1.domain.com

    Any help would be greatly appreciated.

  • I got it to work using the following iRule:

    
    when HTTP_REQUEST {
    set sHost "[getfield [string tolower [HTTP::host]] "." 3]"
    if { [HTTP::host] starts_with "www" }{
    HTTP::redirect https://[getfield [HTTP::host] ":" 1][HTTP::uri]
    } elseif {[string length $sHost] == 0 }{
    HTTP::redirect https://www.[getfield [HTTP::host] ":" 1][HTTP::uri]
    } else {
    HTTP::redirect https://[getfield [HTTP::host] ":" 1][HTTP::uri]
    }
    }