Forum Discussion

Fluidetom_12222's avatar
Feb 02, 2017

Redirect iRule and replace only part of the URI

Hi Guys,

 

I have the following scenario and I'm not sure how to start writing my iRule to handle it. I need to redirect all requests for https://oldsite.domain.com/psp/atlas_1/EMPLOYEExxx/whatever to https://newsite.domain.com/psp/product/EMPLOYEExxx/whatever.

 

My problem is that the "/psp/atlas_1/" can change while everything as of and after "/EMPLOYEExxx/whatever" must remain. "/psp" can be either "psp" or "psc" and must remain as in the original request. "/atlas_1" can be "atlas" or "atlas_X" where X can be any number.

 

So to summarize, what I need to achieve is to extract the "/atlas..." part from the original URI and replace it by "/product/". SO I guess it's a kind of string map with a regex or something like that.

 

Would you have any idea how I could achieve this?

 

Thanks in advance

 

3 Replies

  • Try something like this (untested):

    when HTTP_REQUEST {
    HTTP::uri [string map { [lindex [split [HTTP::uri] "/" ] 2] product} [HTTP::uri]] 
    HTTP::respond 301 Location "https://newsite.domain.com[HTTP::uri]"
    }
    
  • Eventually this is what I implemented.

    Thanks for your help.

    when HTTP_REQUEST {
    
        if { [string tolower [HTTP::host]] equals "oldsite.domain.com" }
            {
                set NEWURI [string map "oldsite newsite" [HTTP::uri]]
                set NEWURI [string map "oldstring newstring" $NEWURI]
                set NEWURL https://newsite.domain.com$NEWURI
                HTTP::respond 200 content "
                
                
                
                Your page has moved !
                
                
                
                
                Your page has moved !
                The website you have requested has been migrated to 
                
                You will be automatically redirected to the new address in 5 seconds.
                Please bookmark the next page.
                
                
                Thank you for your cooperation.
                
                
    
                
                
                
    
                " noserver
                return
    
            }
    

    }

  •   I was looking for the similar one but replace the URI and change the referer as well. can you please help me with that.

     

    when HTTP_REQUEST {

     

       if { [HTTP::uri] starts_with "/abcd" } {

           HTTP::uri [string map {"/abcd" "/xyz"} [HTTP::uri]]

       }

    }