Forum Discussion

Azerturkbank's avatar
Azerturkbank
Icon for Nimbostratus rankNimbostratus
Nov 04, 2019

Redirect IP request to the name irule

Hello friends.

I am experiencing with the issue that I can not handle.

I wrote irule that when someone send request to the f5 by using its IP address like https://10.10.10.15/xxx, redirect it to page https://webserver.test.local/yyy.

But it does not work.

I don`t know whether irule is true or not.

My aid is users to give access only over domain name.

IP address requests should be redirect to the domain name as well.

 

when HTTP_REQUEST {

      if { [string tolower [HTTP::host]] equals "webserver.test.local" } {

         if { [HTTP::uri] equals "/" } {

            HTTP::respond 302 Location "https://[HTTP::host]/yyy/"

          }

      elseif { not ([HTTP::uri] contains "yyyy") } {

      HTTP::respond 302 Location "https://[HTTP::host]/yyy"

           }

              elseif { ([HTTP::host] contains "10.10.10.15") } {

      HTTP::redirect "https://[HTTP::host]/yyy"

             }

         node "10.20.1.15:4443"

3 Replies

  • elseif { ([HTTP::host] contains "10.10.10.15") } {

          HTTP::redirect "https://[HTTP::host]/yyy"

    }

     

    This redirects back to the same host the user entered, which as per the if condition is "10.10.10.15"

    You would want to do HTTP::redirect "https://webserver.test.local/yyy" in this branch.

    Or just do that for all three redirects, because for the first one it doesn't matter and in the second one [HTTP::host] could also be the IP address.

  • Hi

     

    Looks like you would be causing a redirect loop with the lines

     

           elseif { ([HTTP::host] contains "10.10.10.15") } {

          HTTP::redirect "https://[HTTP::host]/yyy"

     

    as you're not changing the host to something other that the IP address.

     

    Also, your call to check the host name of 10.10.10.15 is happening within the if statement to check for the host name of webserver.test.local. Move this logic to its own if statement.

     

    Try this

    when HTTP_REQUEST {
    	if { [string tolower [HTTP::host]] equals "webserver.test.local" } {
    		if { [HTTP::uri] equals "/" } {
    			HTTP::respond 301 Location "https://[HTTP::host]/yyy/"
    			#Stop processing further redirects
    			return
    		} elseif {
    		not ([HTTP::uri] contains "yyy") } {
    			HTTP::respond 301 Location "https://[HTTP::host]/yyy"
    			#Stop processing further redirects
    			return
    		} 
    		}
    		if {
    			([HTTP::host] contains "10.10.10.15") } {
    			HTTP::respond 301 Location "https://webserver.test.local/yyy"
    			#Stop processing further redirects
    			return
    		}
    		#This will send all requests to this server, is this what you want?
    		node "10.20.1.15:4443"
    }

    You might also want to change these lines

     

    not ([HTTP::uri] contains "yyy") } {

    HTTP::respond 301 Location "https://[HTTP::host]/yyy"

    to something like this where you check if /yyy is at the start of the uri to prevent legitimate calls to get redirect eg /something/something_else/yyy.jpg

    not ([HTTP::uri] starts_with "yyy") } {

    HTTP::respond 301 Location "https://[HTTP::host]/yyy[HTTP::uri]"

     

    Also, if these links for /yyy and to the hostname are permanent, consider changing the HTTP status code to 301 (permanent) rather than 302 (temporary) as per my example above

  • I fixed problem like below and derived advantage from your reccomandations:

     

    when HTTP_REQUEST {

          if { [string tolower [HTTP::host]] equals "webserver.test.local" } {

             if { [HTTP::uri] equals "/" } {

                HTTP::respond 301 Location "https://[HTTP::host]/yyy"

    return

              }

          elseif { not ([HTTP::uri] starts_with "/yyy") } {

          HTTP::redirect "https://[HTTP::host]/yyy"

               node "10.20.1.15:4443"

    }

     elseif { ([HTTP::host] starts_with "10.10.10.15") and not ([HTTP::uri] starts_with "/yyy") } {

          HTTP::redirect "https://webserver.test.local/yyy"

               }

    }

    But what is the logic behind the return keyword?