Technical Forum
Ask questions. Discover Answers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Generic irule to redirect www to non-www

dgytech
Altostratus
Altostratus

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]

  }

}

1 ACCEPTED SOLUTION

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

View solution in original post

2 REPLIES 2

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

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