Forum Discussion

Thicardoso21's avatar
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

it should be redirected to

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.

if { [HTTP::uri] contains ""} {
HTTP::redirect "[HTTP::uri]"

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


4 Replies

  • Hi Thicardoso21,

    Returns or sets the URI part of the HTTP request.

    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.

    Returns the query part of the HTTP request. The query is defined as the part of the request past a ? character, if any.

    Returns the value contained in the Host header of an HTTP request.

    For the following URL:

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


    when HTTP_REQUEST {
    	if { ([HTTP::host] eq "" or [HTTP::host] eq "") and [HTTP::uri] starts_with "/content1/"} {
    		HTTP::redirect[string map {"/content1/" "/"} [HTTP::uri]]
    • 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 "") ||([string tolower [HTTP::host]] equals "")) 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 "") and ([string tolower [HTTP::path]] starts_with "/"))}{
             HTTP::redirect "[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 "" , and want to retrieve /Content1/anythingelse or Click on a button on new web page retieves directly /content1/anythingelse  Resource  , so it will appeare "" , 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 . 


      • Thicardoso21's avatar
        Icon for Altostratus rankAltostratus

        Hi Mohamed_Ahmed_Kansoh 

        Answering your question, we would change the old hostname from to, 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
      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.