Forum Discussion

Thiyagu's avatar
Thiyagu
Icon for Cirrus rankCirrus
Jan 20, 2020

Irule for an URI redirection

Hello All,

I have created an irule for an URI redirection and the expected flow as below:

 

1- Client to LB : https://www.domain1.com/resource1

2- LB to Client: Permanent URI redirection to an other URI value >> https://https://www.domain1.com/resource2

3- Client to LB: accessing the resource using the new URI >> https://https://www.domain1.com/resource2

 

Could you please look into the below script and correct if I would have missed anything and suggest me is there anyway I can do it better?

 

===========================================

when HTTP_REQUEST {

   set NEW_URI "resource2"

   switch -glob [string tolower [HTTP::uri]]

   "/resource1"

    {

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

    }

   }

============================================

 

Regards,

Thiyagu

7 Replies

  • Try this:

    when HTTP_REQUEST {
     
       set NEW_URI "/resource2"
     
       switch -glob [string tolower [HTTP::uri]]
     
       "/resource1"
     
        {
     
         HTTP::respond 301 Location "https://[HTTP::host]$NEW_URI"
     
        }
     
       }
    • Thiyagu's avatar
      Thiyagu
      Icon for Cirrus rankCirrus

      Thanks Leonorodo for helping me. For some reason these irule are not working as expected while executing the traffic flow:

       

      ==============================irule 2========To replace the URI to the server

      when HTTP_REQUEST {

          if {[string tolower [HTTP::uri]] starts_with "/resource1"}{

              set newuri [string map -nocase {"/provision" "resource2"}[HTTP::uri]]

              HTTP::uri $newuri

              pool POOL1

              }

          }

      ====================================

       

      ===============irule 3================To redirect back to the client with an new URI value for further communications

      ===================================

            when HTTP_REQUEST {

               set NEW_URI "/resource2"

                 switch -glob [string tolower [HTTP::uri]] {

                   "/resource1" {

                         HTTP::redirect 301 "https://[HTTP::host]$NEW_URI"

                   }

                  }

             }

      =========================================

       

      Could you please help me to fix the issue?

       

      Regards,

      Thiyagu

       

      • Can you define what is not working. Hows the flow going to and fro and what is being seen and what are you expecting. The above shared should do the trick unless something is rewriting it and goes unexpected.

  • "Option 1: all the URI values are replaced with the value of /resource2"

    You mean the query string? or there are also more folders?

     

    Do you the client to be redirect, or juts change the URL on the way to the server?

     

     

    • Thiyagu's avatar
      Thiyagu
      Icon for Cirrus rankCirrus

      Thanks Leonardo. Chaging the URI to the server can also help me to fix the issue.

       

      Regards,

      Thiyagu

  • "Option 1: all the URI values are replaced with the value of /resource2"

     

    You mean the query string? or there are also more folders?