Forum Discussion

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

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....
  • Enes_Afsin_Al's avatar
    Oct 20, 2020

    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
    	}
    }