Forum Discussion

dgytech's avatar
dgytech
Icon for Altostratus rankAltostratus
Oct 20, 2020
Solved

Generic irule to redirect www to non-www

Hi all - hoping this is a simple one here... i'm looking to create a generic irule to redirect www traffic to non-www traffic. In the example below, it redirects back to the original www. and loops. Is there a way to exclude "www." in the HTTP::redirect ?

I really do not want to have to create a host specific irule every time. Thank you for any assistance here.

 

when HTTP_REQUEST {

 if {[string tolower [HTTP::host] ] starts_with "www." }{

   HTTP::redirect https://[HTTP::host][HTTP::uri]

  }

}

  • Hi dgytech,

    1. string map

    when HTTP_REQUEST {
    	if { [HTTP::host] starts_with "www." } {
    		HTTP::redirect https://[string map {www. ""} [HTTP::host]][HTTP::uri]
    		return
    	}
    }

    2. string range

    when HTTP_REQUEST {
    	if { [HTTP::host] starts_with "www." } {
    		HTTP::redirect https://[string range [HTTP::host] 4 end][HTTP::uri]
    		return
    	}
    }

    3. getfield

    when HTTP_REQUEST {
    	if { [HTTP::host] starts_with "www." } {
    		HTTP::redirect https://[getfield [HTTP::host] "www." 2][HTTP::uri]
    		return
    	}
    }

    4. substr

    when HTTP_REQUEST {
    	if { [HTTP::host] starts_with "www." } {
    		HTTP::redirect https://[substr [HTTP::host] 4][HTTP::uri]
    		return
    	}
    }

2 Replies

  • Hi dgytech,

    1. string map

    when HTTP_REQUEST {
    	if { [HTTP::host] starts_with "www." } {
    		HTTP::redirect https://[string map {www. ""} [HTTP::host]][HTTP::uri]
    		return
    	}
    }

    2. string range

    when HTTP_REQUEST {
    	if { [HTTP::host] starts_with "www." } {
    		HTTP::redirect https://[string range [HTTP::host] 4 end][HTTP::uri]
    		return
    	}
    }

    3. getfield

    when HTTP_REQUEST {
    	if { [HTTP::host] starts_with "www." } {
    		HTTP::redirect https://[getfield [HTTP::host] "www." 2][HTTP::uri]
    		return
    	}
    }

    4. substr

    when HTTP_REQUEST {
    	if { [HTTP::host] starts_with "www." } {
    		HTTP::redirect https://[substr [HTTP::host] 4][HTTP::uri]
    		return
    	}
    }
    • dgytech's avatar
      dgytech
      Icon for Altostratus rankAltostratus

      This is fantastically fantastic! I went with option #1, works perfectly. Thank you!!