Forum Discussion

Grayson_149410's avatar
Grayson_149410
Icon for Nimbostratus rankNimbostratus
Sep 29, 2015

irule remove "www."

Does anyone have an iRule that will remove the "www." part of a URL? I tried the following iRule, but it does not work.

when HTTP_REQUEST {
 if { [HTTP::uri] equals "https://www.abc.xyz.com" } {
 HTTP::redirect "https://abc.xyz.com"
 }
}

Edit: Also, the site is certed using a *.abc.com wildcard certificate.

5 Replies

  • Try this:

    when HTTP_REQUEST {
        if {[string tolower [HTTP::host]] starts_with "www."}{
            HTTP::redirect "https://[string map {www. ""} [string tolower [HTTP::host]]]"
        }
    }
    
    • Brad_Parker_139's avatar
      Brad_Parker_139
      Icon for Nacreous rankNacreous
      if you want to keep the original URI use this when HTTP_REQUEST { if {[string tolower [HTTP::host]] starts_with "www."}{ HTTP::redirect "https://[string map {www. ""} [string tolower [HTTP::host]]][HTTP::uri]" } }
  • Try this:

    when HTTP_REQUEST {
        if {[string tolower [HTTP::host]] starts_with "www."}{
            HTTP::redirect "https://[string map {www. ""} [string tolower [HTTP::host]]]"
        }
    }
    
    • Brad_Parker's avatar
      Brad_Parker
      Icon for Cirrus rankCirrus
      if you want to keep the original URI use this when HTTP_REQUEST { if {[string tolower [HTTP::host]] starts_with "www."}{ HTTP::redirect "https://[string map {www. ""} [string tolower [HTTP::host]]][HTTP::uri]" } }
  • From an SEO perspective, a 301 redirect would be best;-

    when HTTP_REQUEST {
       if {[string tolower [HTTP::host]] starts_with "www." } {
          HTTP::respond 301 Location "https://[substr [HTTP::host] 4][HTTP::uri]" content "" noserver
          return
       }
    }
    

    Alternatively, if you want the browser to continue to display the www. Host variant, you may want to proxy the request with a modified Host header rather than redirecting;

    when HTTP_REQUEST {
       if {[string tolower [HTTP::host]] starts_with "www." } {
          HTTP::host [substr [HTTP::host] 4]
       }
    }