Forum Discussion
Extracting/Removing part of a URI
Hello,
Apologies if this has already been asked, however is it possible to extract part of a URI?
For example; an incoming URL may be:
http://originalurl.com/pages/index.htm/2013/08/15/mypost
and we want it to 301 redirect it TO:
http://newurl.com/2013/08/15/mypost
So we'd want to change the host (which is easy enough), however we want to drop the /pages/index.htm part of the original URI while maintaining anything after that; which in this example is /2013/08/15/mypost
Does anyone know if this is even possible?
Any guidance would be kindly appreciated.
Thank you
13 Replies
- nitass
Employee
can we use "string map"?
e.g.
% set uri "/pages/index.htm/2013/08/15/mypost" /pages/index.htm/2013/08/15/mypost % put [string map {/pages/index.htm/ /} $uri] /2013/08/15/mypost - Kevin_Stewart
Employee
Is the "/pages/index.htm" portion static (always the same), or variable? Is there a pattern to the URL scheme other than removing the top two levels from the path?
- mbuk2k_59598
Nimbostratus
Thank you for your immensely speedy response both!
Yes correct Kevin, that part is always static.
Hmm no I don't suppose there is, all of this part: http://originalurl.com/pages/index.htm would need stripping and replacing with the new domain (carrying through the remainder of the original URI, if that makes sense)?
Nitass; wouldn't that need each and every potential URI inputting in to the iRule if you're hard coding the dates, etc?
- nitass
Employee
e.g.
[root@ve11a:Active:Changes Pending] config tmsh list ltm virtual bar ltm virtual bar { destination 172.28.20.111:80 ip-protocol tcp mask 255.255.255.255 pool foo profiles { http { } tcp { } } rules { myrule } source 0.0.0.0/0 source-address-translation { type automap } vs-index 23 } [root@ve11a:Active:Changes Pending] config tmsh list ltm rule myrule ltm rule myrule { when HTTP_REQUEST { if { [HTTP::host] equals "originalurl.com" } { if { [HTTP::path] starts_with "/pages/index.htm/" } { HTTP::respond 301 noserver Location "http://newurl.com[string map {/pages/index.htm/ /} [HTTP::uri]]" } } } } [root@ve11a:Active:Changes Pending] config curl -I http://172.28.20.111/pages/index.htm/2013/08/15/mypost -H "Host: originalurl.com" HTTP/1.0 301 Moved Permanently Location: http://newurl.com/2013/08/15/mypost Connection: Keep-Alive Content-Length: 0 - mbuk2k_59598
Nimbostratus
Thanks Nitass, I'll give that a shot!
- Jeff_Knights_44
Nimbostratus
what version is this written for?
I am in using version 11.3 HF8 (3144) and I need to do the exact same thing....
- Jeff_Knights_44
Nimbostratus
I actually need to go from this "www.url.com/restricted/whatever/anything.htm to www.url.com/whatever/anything.htm by simply removing a single piece of the uri "restricted" but leaving everything before and after it.
is this pretty easy?
- Kevin_Stewart
Employee
The string map function should work in any version.
when HTTP_REQUEST { if { [string tolower [HTTP::uri]] starts_with "/restricted" } { HTTP::uri [string map {"/restricted" ""} [HTTP::uri]] } } - Jeff_Knights_44
Nimbostratus
now to do a list of different possibilities I would just make a list of them? and make the replaceable part a variable?
- Jeff_Knights_44
Nimbostratus
heres what I did:
when HTTP_REQUEST {
if { [string tolower [HTTP::uri]] starts_with "/restricted"
} {
HTTP::uri [string map {"/restricted" ""} [HTTP::uri]]
}
}
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
