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

Url redirection

mj16othman
Altostratus
Altostratus

Hi guys I need your need.

Let's say I have a url (example.com/apps/test.)

I want this exact url to work, and anything else that's not that url to redirect to (example.com)

7 REPLIES 7

Hi @mj16othman , 
Try this : 

when HTTP_REQUEST {
if { [string tolower "[HTTP::host][HTTP::path]"] contains "example.com/apps/test" }{
	return 
	} else { 
	
	#HTTP::redirect "http://example.com"
	HTTP::redirect "http://[HTTP::host]"
	}
}

> you must assign the default pool to that virtual server. 
use only one of those redirections  : 
HTTP::redirect "http://example.com"
HTTP::redirect "http://[HTTP::host]"

I hope this helps you

_______________________
Regards
Mohamed Kansoh

The iRule posted by  should work, but you can clean it up a bit. Here we do the following:- More efficient string comparison using starts_with rather than contains. Also, more secure as a URL may be odd and repeat that string as part of URI path (unlikely though).

- Removed unnecessary comparison for host header. Likely there is no virtual hosting, so all (and only) example.com traffic hitting the virtual server. We only need it for return if path doesn't match our pattern.

- Support both https and http protocols. In case you perform or what to add SSL termination or what not.

- You could also get fancier and support multiple paths you want to check via data groups, or even use a variable for the comparison string portion

So here we have it.

when HTTP_REQUEST {
if { [string tolower [HTTP::path]] starts_with "/apps/test" }{
return
} else {
if { [TCP::local_port] == 443 }{
HTTP::redirect "https://[HTTP::host]"
} else {
HTTP::redirect "http://[HTTP::host]"
}
}
}

@Mohamed_Ahmed_Kansoh

Sorry for the formatting, but posting TCL code via the iPhone triggers an error on DevCentral!

"Correct the highlighted errors and try again.

Post flooding detected (community received posts of a unique message more than 1 times within 3,600 seconds)"

Thats even with just a simple "test" using the TCL </> option in the WYSIWYG editor. Strange.

 

Tagging @LiefZimmerman so he can see your issue with sharing TCL code from your phone triggering a post flooding timeout. 

Hi @whisperer , 
Thanks a lot , 
I have tested it in my internal Lab , it worked as expected for HTTP only however he can do the same for HTTPS but with offloading and redirecting to https at least. 
@mj16othman 

_______________________
Regards
Mohamed Kansoh

@Mohamed_Ahmed_Kansoh Correct, for HTTPS you would need to have an appropriate VS with client SSL profile and listening on standard port 443. I always think about writing generic iRules so you can resume them for multiple applications.

@whisperer , 
I got you , well I like this way to normalize everything to be generic 😉

_______________________
Regards
Mohamed Kansoh