For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

Forum Discussion

onlineops's avatar
onlineops
Icon for Nimbostratus rankNimbostratus
May 01, 2018

irule: root domain (only) non-www to www redirect

I have a series of websites that need a non-www to www redirect, bu I still need to support direct subdomains without a redirect.

Example:

  1. http://abc.com redirects to http://www.abc.com
  2. http://bank.abc.com does not redirect and stays at http://bank.abc.com

Assume:

  • I have a bunch of domains here and don't want write a conditional statement by domain, it's too brittle
  • I have several subdomains to support and would prefer not to conditionally address each, but can if necessary
  • I need to do http and https

A simple irule that handles 1, but not 2, http only:

    when HTTP_REQUEST {
  if { ! ([HTTP::host] starts_with "www.") } {
    HTTP::redirect http://www.[HTTP::host][HTTP::uri]
  }
}

What's an efficient way to say "redirect root domains only" ?

2 Replies

  • OM's avatar
    OM
    Icon for Altocumulus rankAltocumulus

    Hi, fo a specific domain irule, you can use:

     

    when HTTP_REQUEST { if { ([HTTP::host] eq "abc.com")}{ HTTP::redirect .[HTTP::host][HTTP::uri] } }

     

    For global domain irule, you might need a regex, maybe something like this:

     

    when HTTP_REQUEST { if { ([HTTP::host] eq "^[^.]+$.com") } { HTTP::redirect .[HTTP::host][HTTP::uri] } }

     

  • That rule will work for the TLD (Top Level Domains) .com - and again, if I don't want to get specific about doing "OR" for all the possible TLDs, I think this works, regardless of top level domain:

    when HTTP_REQUEST {
      if { ([HTTP::host] matches_regex {^[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$}) } { HTTP::redirect http://www.[HTTP::host][HTTP::uri] } 
    }
    
    
    when HTTP_REQUEST {
      if { ([HTTP::host] matches_regex {^[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$}) } { HTTP::redirect https://www.[HTTP::host][HTTP::uri] } 
    }