Forum Discussion

Thicardoso21's avatar
Thicardoso21
Icon for Altostratus rankAltostratus
Nov 12, 2022

Redirect irule from second field

I'm struggling to try to acehive this one. So, any help would be much appreciated.

When receive an HTTP request.

If url contais https://mysite.com/content1/anythingelse.html

it should be redirected to https://newsite.com/anythingelse.html

Basically everything after /content1/ would remain the same.

It looks like simple but as a newbie in iRules, I'm struggling to understand how sould I make it.

I thoung i something like this.

when HTTP_REQUEST {
if { [HTTP::uri] contains "mysite.com/content1/anythingelse.html"} {
HTTP::redirect "https://newsite.com/[HTTP::uri]"
}
}

THis iRule would be created in a F5 running code 16.2.

Thanks

4 Replies

  • Hi Thicardoso21,

    [HTTP::uri]
    Returns or sets the URI part of the HTTP request.
    https://clouddocs.f5.com/api/irules/HTTP__uri.html

    [HTTP::path]
    Returns or sets the path part of the HTTP request. The path is defined as the path and filename in a request. It does not include the query string.
    https://clouddocs.f5.com/api/irules/HTTP__path.html

    [HTTP::query]
    Returns the query part of the HTTP request. The query is defined as the part of the request past a ? character, if any.
    https://clouddocs.f5.com/api/irules/HTTP__query.html

    [HTTP::host]
    Returns the value contained in the Host header of an HTTP request.
    https://clouddocs.f5.com/api/irules/HTTP__host.html

    For the following URL:
    https://mysite.com/content1/anythingelse.html?a=123

    [HTTP::uri]		: /content1/anythingelse.html?a=123
    [HTTP::path]	: /content1/anythingelse.html
    [HTTP::query]	: a=123
    [HTTP::host]	: mysite.com

    iRule:

    when HTTP_REQUEST {
    	if { ([HTTP::host] eq "mysite.com" or [HTTP::host] eq "www.mysite.com") and [HTTP::uri] starts_with "/content1/"} {
    		HTTP::redirect https://newsite.com/[string map {"/content1/" "/"} [HTTP::uri]]
    		return
    	}
    }
    
    • Hi Thicardoso21 , 
      As Enes_Afsin_Al  sent it will be sufficient for your request exactly. 

      > May I have understood your request in a different way , please correct for me , I think you need to change the old " Host name " FQDN each time regardless /content1/ exists on request or not. 
      so I have created and irule matchs your request , also it prevents the old Host name and refirect to the new hostname  each time anyone put the old url. 
      So I wrote this iRule : 

      when HTTP_REQUEST {
           if {(([string tolower [HTTP::host]] equals "mysite.com") ||([string tolower [HTTP::host]] equals "newsite.com")) and ([string tolower [HTTP::path]] starts_with "/content1")}{
                HTTP::redirect https://newshopping.asm.f5/[string map {"/content1" "/"} [HTTP::path]]
                } else {
            if {(([string tolower [HTTP::host]] equals "mysite.com") and ([string tolower [HTTP::path]] starts_with "/"))}{
             HTTP::redirect "https://newsite.com[HTTP::path]"
             }
      }
      }

       > I added this Part :  ||([string tolower [HTTP::host]] equals "newshopping.asm.f5")
      to eliminate any possibility to see " Content1 " with old or new hostname , I though that if you are redirected to the new web site " newsite.com" , and want to retrieve /Content1/anythingelse or Click on a button on new web page retieves directly /content1/anythingelse  Resource  , so it will appeare " newsite.com/content1/anythingelse" , So I have put this condition to eliminate the existance of /content1/ resource with old hostname and new as well. 

      > After " else " Statement , it is to redirect all requests to old host name to new host name regardless what are the rest of uri path. 
      you can remove it if you want to redirect old hostname to new hostname with only existance of /Content1/  resource path in Requests . 

      Regards 

      • Thicardoso21's avatar
        Thicardoso21
        Icon for Altostratus rankAltostratus

        Hi Mohamed_Ahmed_Kansoh 

        Answering your question, we would change the old hostname from mysite.com to newsite.com, only if /content1 is present. If not, redirect is not required.

        Thanks for adding those extra parts and explain your code. It looks great!
        I will also setup this one in our lab and test it next week. I keep you posted.

        Many thanks..

    • Thicardoso21's avatar
      Thicardoso21
      Icon for Altostratus rankAltostratus

      Hi Enes_Afsin_Al 
      Thanks a lot for explanaition and sharing those helpful links. I will certaily read them to have a better understanding.

      I will be able to test this next week. I will try your suggestion and let you now.

      Thank you.