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:

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

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

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?

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?

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

Thanks Nitass, I’ll give that a shot!

what version is this written for?

I am in using version 11.3 HF8 (3144) and I need to do the exact same thing…

I actually need to go from this "URL.com - MediaOptions to URL.com - MediaOptions by simply removing a single piece of the uri “restricted” but leaving everything before and after it.

is this pretty easy?

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]]
    }
}

now to do a list of different possibilities I would just make a list of them? and make the replaceable part a variable?

heres what I did:

when HTTP_REQUEST {

if { [string tolower [HTTP::uri]] starts_with “/restricted”

} {

HTTP::uri [string map {“/restricted” “”} [HTTP::uri]]

}

}

sorry about all the replies, im new at this…

I basically made a list of the 6 different variations they were looking for and repeated everything but the actual word in the directory structure they were looking for.

now to do a list of different possibilities I would just make a list of them? and make the replaceable part a variable?

Are you referring to a list of different URI patterns to replace? If so, just add them in pairs to the [string map ] command:

[string map {"/foo" "" "/bar" ""} [HTTP::uri]]

wow, that’s a lot easier