Forum Discussion

ant77's avatar
ant77
Icon for Cirrostratus rankCirrostratus
Feb 22, 2021

Redirect to new URL, but keep certain part of the original URI path.

Hi All,

 

Can you guys help with this unique requirement? We need to match based on the URI the client comes in on (IAM), than redirect to a new URL while

changing IAM to IAM2, and keeping their unique URI hash...

 

Thanks for your help in advance!

 

when HTTP_REQUEST {    

if { [string tolower [HTTP::host]] equals "test.abc.com" and [string tolower [HTTP::uri]] starts_with "/IAM/" } { 

       set temp_uri [HTTP::uri]

       HTTP::respond 307 Location "https://new.abc.com/IAM2$temp_uri" 

   }

}

 

 

Requirements:

 

  1. match URI "/IAM" on the original URL
  2. Redirect the user to a new URL, however change the URI "IAM" to "IAM2" while taking the dynamic generated URI hash using the "set temp_uri"

and appending this to the new redirected to URL...

so basically anything after IDP should be carried over to the new URL as this is dynamically generated and can change. We need this...

"IDP/SSO.saml2?SAMLRequest=nZBNT4QwEIb%2FStN7WVo%2BCg2wqeAmJGqMr123ab%"

 

 

Example:

Original URL--> https://test.abc.com/IAM/IDP/SSO.saml2?SAMLRequest=nZBNT4QwEIb%2FStN7WVo%2BCg2wqeAmJGqMr123ab%

 

New Redirect to -> Should redirect to https://new.abc.com/IAM2/IDP/SSO.saml2?SAMLRequest=nZBNT4QwEIb%2FStN7WVo%2BCg2wqeAmJGqMr123ab%

 

 

The issue with the code above is that when we try this, the final redirect URL also appends the orginal "IAM" into the URL string. Is there a way to not do that and only add this and also keep anything after the "/IDP/SSO.saml2?......." as this is dynamic?

IAM2/IDP/SSO.saml2?SAMLRequest=nZBNT4QwEIb%2FStN7WVo%2BCg2wqeAmJGqMr123ab%

 

It adds the "IAM" as you can see below.....

https://new.abc.com/IAM2/IAM/IDP/SSO.saml2?SAMLRequest=nZBNT4QwEIb%2FStN7WVo%2BCg2wqeAmJGqMr123ab%

 

 

5 Replies

  • Hi ant77,

    when HTTP_REQUEST {
    	if { [HTTP::host] equals "test.abc.com" and [HTTP::uri] starts_with "/IAM/" } {
    		set temp_uri [string map {/IAM/ /IAM2/} [HTTP::uri]]
    		HTTP::respond 307 Location "https://new.abc.com$temp_uri"
    	}
    }
    • ant77's avatar
      ant77
      Icon for Cirrostratus rankCirrostratus

      This works! Thank you very much Enes Afsin Al...

      Appreciate your help!

  • ant77's avatar
    ant77
    Icon for Cirrostratus rankCirrostratus

    Ok here is a strange thing and it is not working with HTTPS...

    I tried the modification tonight and it works on a HTTP Virtual server, however when I tried the same iRule on an HTTPS VIP, it does not work.

    Do you know what is happening? or is the URI string mapping not working with HTTPS? Is it possible the F5 can't read the URI (IAM) in the path and can't change the URI to IAM2 and redirect because of that? Do I need to do anything or consolidate the two if statement together?

    The bottom one works if you direct go to https://test.abc.com/123/abc, but for some reason it does not like the matching of IAM to IAM2....what could be the issue?

    Thanks for your help.

    when HTTP_REQUEST {
    	if { [HTTP::host] equals "test.abc.com" and [HTTP::uri] starts_with "/IAM/" } {
    	set temp_uri [string map {/IAM/ /IAM2/} [HTTP::uri]] 
    		HTTP::respond 307 Location "https://site1.company.com$temp_uri" }
    		log local0. "Host [HTTP::host] --> URI [HTTP::uri] -->Redirected"
     
         if { [HTTP::host] equals "test.abc.com" } {
            set temp_uri [HTTP::uri] 
            HTTP::respond 307 Location "http://site2.company.com$temp_uri" 
            log local0. "Host [HTTP::host] --> URI [HTTP::uri] -->Redirected"	
      }
    }
  • I think two if matching cause this. Can you try this?

    when HTTP_REQUEST {
    	if { [HTTP::host] equals "test.abc.com" } {
    		#log local0. "Host [HTTP::host] --> URI [HTTP::uri] --> Redirected"
    		if { [HTTP::uri] starts_with "/IAM/" } {
    			HTTP::respond 307 Location "https://site1.company.com[string map {/IAM/ /IAM2/} [HTTP::uri]]"
    			return
    		}
    		else {
    			HTTP::respond 307 Location "http://site2.company.com[HTTP::uri]"
    			return
    		}
    	}
    }
    • ant77's avatar
      ant77
      Icon for Cirrostratus rankCirrostratus

      Enes Afsin Al, Thank you so much! I will try tonight and let you know...