Forum Discussion
iRule redirect but need some of the original PATH
Relatively new to iRules but have done a ton of scripting (PoSh, vbs, etc...) and understand the basics and have studied the http::uri, http::path and http::query built-in variables - thanks in advance for your assistance!
I have an incoming URL like "https://www.mycompany.com/management/dept/eng/corporatecommunications/Shared%20Documents/Mine.docx?OpenDocument"
The query parameter (OpenDocument) may or may not exist - need to plan for both.
I need the redirect URI to be: "https://www.mycompany.com/sites/corpcomm/Shared%20Documents/Mine.docx?OpenDocument"
I don't have a BIGIP to test with so I've built a few PoSh functions to create the http::uri, http::path, etc... variables to work against a string - not apples to apples but is shedding light on my problem.
I feel like I should be able to capture "/Shared%20Documents/Mine.docx?OpenDocument" in a couple of variables but uri::path depth isn't easy to replicate in PoSH. "/Shared%20Documents/" could be another layer or two or six deep - the text itself is just an example.
This is one scenario of many to follow with the same basic convention - we're using a switch statement now: switch -glob [string tolower [HTTP::uri]] but we are open to suggestions...
Thanks!
5 Replies
- Stanislas_Piro2
Cumulonimbus
Hi,
there are 2 solutions:
string map to replace /management/dept/eng/corporatecommunications/ by /sites/corpcomm/
HTTP::uri [string map "/management/dept/eng/corporatecommunications/ /sites/corpcomm/" [HTTP::uri]]string range to strip the begin of URI (45 first characters) and replace it by the new one:
HTTP::uri /sites/corpcomm/[string range [HTTP::uri] 45 end]the second one is the most optimized for performances.
Hi Dennis,
the
command is ideal for content switching scenarios, where you have to evaluate 10-50 different URLs in a first-match condition. If you need to evaluate less than 10 different URLs, then the[switch]
command would have some minor performance benefits, but would be a little more complex. And if you need to evaluate more than 50 different URLs, then the[if]
command in combination with data groups may come to its shine![class]The
in combination with[string tolower [HTTP::uri]]
is also a very beginner friendly way to parse the URLs, since it may contain easy to understand String-Wildcards-glob
or Single-Letter-Wildcards*
which can be used as a trailing or leading wildcards, in combination with each other or even for both sides or in between of an URL search string.$A
based[switch]
evaluation followed by an redirect would look like that...[HTTP::uri]when HTTP_REQUEST { switch -glob -- [string tolower [HTTP::uri]] "*/shared%20documents/mine.docx*" { HTTP::redirect "/sites/corpcomm/Shared%20Documents/Mine.docx?OpenDocument" } "*/corporatecommunications/shared%20documents/*" { HTTP::redirect "/sites/corpcomm/Shared%20Documents/" } default { Do nothing or select a pool } }Cheers, Kai
Hi Dennis,
post your current code and we will help out... ;-)
Cheers, Kai
- Dennis_Goss_70_
Nimbostratus
Thanks - the commented line works but redirects everything to the root of the site at the new location - not ideal since most folks love their deep link bookmarks...
The last line is my latest attempt - not tested yet so it may be a winner? Thanks for the help!
when HTTP_REQUEST { switch -glob [string tolower [HTTP::uri]] {
"/oas/ops/programs*" { HTTP::redirect "https://www.mycompany.com[HTTP::uri]" } "/offices/management/ocio/eng/tacticalcommunications*" { HTTP::redirect "https://www.mycompany.com/sites/taccom" } "/offices/management/ocio/eng/tacticalcommunications*" { HTTP::redirect [string map {"/offices/management/ocio/eng/tacticalcommunications/" "/sites/taccom/"}[HTTP::uri]]}} }
Hi Dennis,
the code looks good. But i duno if you want to have relative redirects based on your website root. If you want a fixed https://* redirect, then take a look to the last line below...
when HTTP_REQUEST { switch -glob [string tolower [HTTP::uri]] { "/oas/ops/programs*" { HTTP::redirect "https://www.mycompany.com[HTTP::uri]" } Redirects http://* to http://* and https:// to https://* "/offices/management/ocio/eng/tacticalcommunications*" { HTTP::redirect [string map {"/offices/management/ocio/eng/tacticalcommunications/" "/sites/taccom/"}[HTTP::uri]]} Redirects http://* and https://* to https://* with -nocase option "/offices/management/ocio/eng/tacticalcommunications*" { HTTP::redirect "https://www.mycompany.com[string map -nocase {"/offices/management/ocio/eng/tacticalcommunications/" "/sites/taccom/"} [HTTP::uri]]"} } }P.s.: In addition it contains a [string map -nocase] syntax to support case-insensitive deep-links. I guess it would be much more user friendly, isn't it? 😉
Cheers, Kai
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com