Forum Discussion

mj16othman's avatar
mj16othman
Icon for Altostratus rankAltostratus
Jun 07, 2023

Url redirection

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

  • 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

    • whisperer's avatar
      whisperer
      Icon for MVP rankMVP

      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

      • whisperer's avatar
        whisperer
        Icon for MVP rankMVP

        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.