Forum Discussion

habib_Khan's avatar
habib_Khan
Icon for Nimbostratus rankNimbostratus
Jul 20, 2016

Need to replace part of uri in url. Tried many irules

Hi All,

I have been trying to replace a part uri. Whenever request comes and URL has uri /sites/mkt/CPTeam/, it should be replaced with /sites/cbtit/. Tried below irules, doesnt work. Anything if i am missing. Only /sites/mkt/CPTeam/ should be replaced rest all uri should remain same.

http://mydocs.dcba.com/sites/mkt/CPTeam/default.aspx http://mydocs.dcba.cpm/sites/mkt/cpteam/_layouts/viewlsts.aspx http://mydocs.dcba.com/sites/mkt/cpteam/inn/SitePages/Home.aspx http://mydocs.dcba.com/sites/mkt/CPTeam/DOH/default.aspx

Irule 1: when HTTP_REQUEST { log local0. "Incoming URI = [HTTP::uri]"

if { [string tolower [HTTP::uri]] starts_with "/sites/mkt/CPTeam/" } {
    set uri [string map -nocase {"/sites/mkt/CPTeam/" "/sites/cbtit/"} [HTTP::uri]]
    log local0. "New URI = $uri"
    HTTP::uri $uri
}

}

Irule 2:

when HTTP_REQUEST { if {[HTTP::uri] contains "/sites/mkt/CPTeam/"} { HTTP::uri [string map {"/sites/mkt/CPTeam/" "/sites/cbtit/"} [HTTP::uri]] } }

Irule 3:

when HTTP_REQUEST { if { [HTTP::uri] starts_with "/sites/mkt/CPTeam/"}{ HTTP::redirect "http://[string map {/sites/mkt/CPTeam/ /sites/cbtit/} [string tolower[HTTP::host]] [HTTP::uri]]" } }

Irule 4: when HTTP_REQUEST { log local0. "Incoming URI = [HTTP::uri]" if { [HTTP::host] contains "mydocs.dcba.com/" } { if {[string tolower [HTTP::uri]] starts_with "/sites/mkt/CPTeam/" } { set newpath [HTTP::host][string map {"/sites/mkt/CPTeam/" "/sites/cbtit/"} [HTTP::uri]] log local0. "New URI = $uri" HTTP::redirect "http://$newpath" } } }

2 Replies

  • what does "doesn't work" mean? what error do you get? do you see happening what you want in your logging?

     

  • ok, so iRule 1, you say string to lower and then check against a string with captitals, that won't work. either remove the string to lower (wouldnt do that) or replace the string without capitals "/sites/mkt/cpteam/"

     

    iRule 2 seems to do what you want, i would add some logging to show that. if it then "doesn't work" then it might be an error in what you replace or the application not handling the rewriting well. you need to provide more info to look into that

     

    iRule 3 and 4 use a redirect that usually is a different approach then rewriting and might open all kind of other issues.

     

    by now i have another question, why are you doing this? isn't it easier just to use how it should be, rewriting adds complexity you want to prevent if not really needed.