Forum Discussion

Erich_Macbeth_2's avatar
Erich_Macbeth_2
Icon for Nimbostratus rankNimbostratus
Dec 01, 2016

Using an iRule for a 301 redirect and replace a sub string within the URI

Hello,

I am trying to do a 301 redirect to a new URL while rewriting a sub string within the original URI on a LTM using an iRule. Here is the simplified example of what I am trying to accomplish:

Original URL - xyz.com/abc/efg/*

301 Redirect URL - tuv.com/123/456/*

The wildcard sting after the sub string I want to replace must remain the same between the original URL and the 301 URL.

This is what I have written up for an iRule:

when HTTP_REQUEST {

if { [HTTP::uri] contains "/abc/efg" } {
     [HTTP::host] replace "tuv.com"     
     [HTTP::uri] replace [string map -nocase {"/abc/efg" "/123/456"} [HTTP::uri]]
     HTTP::respond 301 "https://[HTTP::host][HTTP::uri]"        
   }

}

Thanks in advance and any advise is greatly appreciated.

2 Replies

  • when HTTP_REQUEST {
        if { [string tolower [HTTP::uri]] starts_with "/abc/efg" } {
            set newuri [string map -nocase {"/abc/efg" "/123/456"} [HTTP::uri]]
            HTTP::respond 301 "https://tuv.com${newuri}"        
        }
    }
    
  • Right. I forgot to add the Location header:

    when HTTP_REQUEST {
        if { [string tolower [HTTP::uri]] starts_with "/abc/efg" } {
            set newuri [string map -nocase {"/abc/efg" "/123/456"} [HTTP::uri]]
            HTTP::respond 301 Location "https://tuv.com${newuri}"        
        }
    }