Forum Discussion
Need to parse the [HTTP::uri] and modify it for a redirect
- Dec 29, 2017
could you please explain what exactly the code is doing especially the regular expression part and the string map ?
/~ is literal chars (i.e. it matches /~)
%[^/] matches every chars but not / and the matching string will be stored in variable specified later (e.g. it matches geverest and geverest will be stored in first variable)
[scan [HTTP::uri] {/~%[^/]} first] returns number of matching string. so, we can use it to determine whether it is uri we are interested (i.e. 1).
[string map "/~$first/ /" [HTTP::uri]] replace /~$first/ with / in uri (e.g. it replaces /~geverest/ with /).
[root@bip1a:Active:In Sync] config tclsh % set uri "/~geverest/home/data-modeling-course/course-flyer" /~geverest/home/data-modeling-course/course-flyer % scan $uri {/~%[^/]} first 1 % put $first geverest % string map "/~$first/ /" $uri /home/data-modeling-course/course-flyer
may this be usable?
e.g.
// config
[root@ve13a:Active:In Sync] config tmsh list ltm virtual bar
ltm virtual bar {
destination 172.28.24.10:80
ip-protocol tcp
mask 255.255.255.255
profiles {
http { }
tcp { }
}
rules {
qux
}
source 0.0.0.0/0
translate-address enabled
translate-port enabled
vs-index 4
}
[root@ve13a:Active:In Sync] config tmsh list ltm rule qux
ltm rule qux {
when HTTP_REQUEST {
if { [scan [HTTP::uri] {/~%[^/]} first] } {
HTTP::respond 301 noserver Location "http://[HTTP::host][string map "/~$first/ /" [HTTP::uri]]"
}
}
}
// test
[root@ve13a:Active:In Sync] config curl -I http://172.28.24.10/~geverest/home/data-modeling-course/course-flyer
HTTP/1.0 301 Moved Permanently
Location: http://172.28.24.10/home/data-modeling-course/course-flyer
Connection: Keep-Alive
Content-Length: 0
[root@ve13a:Active:In Sync] config curl -I http://172.28.24.10/~helloworld/home/data-modeling-course/course-flyer
HTTP/1.0 301 Moved Permanently
Location: http://172.28.24.10/home/data-modeling-course/course-flyer
Connection: Keep-Alive
Content-Length: 0
Thank you, This is awesome - does exactly what I need. I am not that great with irules at this level - could you please explain what exactly the code is doing especially the regular expression part and the string map ?
`when HTTP_REQUEST {
if { [scan [HTTP::uri] {/~%[^/]} first] } {
HTTP::respond 301 noserver Location "http://[HTTP::host][string map "/~$first/ /"
[HTTP::uri]]"
}
}`
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