Forum Discussion

MikeM_44778's avatar
MikeM_44778
Icon for Nimbostratus rankNimbostratus
Jan 16, 2007

Rewrite URL Context

How would I go about rewriting the following URL:

 

http://www.testsite.com/scripts/org.test.dispatch?app=uservariable

 

 

to go to

 

 

http://www.testsite.com/webapps/newsite/org.test.dispatch?app=uservariable

 

  • In its simplest form with no error-checking:

    
    when HTTP_REQUEST {
      HTTP::uri "/webapps/newsite/[findstr [HTTP::uri] "/scripts/" 9]"
    }

    I'd build some protection around this if your scope is beyond testing functionality, perhaps verify the path begins correctly:

    
    when HTTP_REQUEST {
      if { [string tolower [HTTP::uri]] starts_with "/scripts/" } {
        HTTP::uri "/webapps/newsite/[findstr [string tolower [HTTP::uri]] "/scripts/" 9]"
      }
    }

  • Another option would be to go with the "string map" command

    when HTTP_REQUEST {
      HTTP::uri [string map -nocase {"/scripts/" "/webapps/newsite/"} [HTTP::uri]]
    }

    This will replace (map) all occurrances of "/scripts/" in the HTTP::uri with the string "/webapps/newsite/". If "/scripts" isn't present in the HTTP::uri the string map returns the original string.

    -Joe
  • I didn't mean to point out that your approach wasn't clean. I just wanted to show a different alternative. Not every solution is right for each environment. My solution will cause odd behavior if the string "/scripts/" is embedded multiple places in the URI so in that case, your solution would work better as it replaces the first 9 characters. Sanity checks are always a good thing...

     

     

    -Joe
  • I wasn't offended in the least. I was glad to see an alternate solution to add to my archive.