Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Answers

iRule Rewrit URI

Hello i' m has newbee in BigIp world.

I need to modify an URI in the following way:

URi Users = https://hostname/(S(session number))/blabla/blabla.xxx

It is possible to rewrite this URI with iRules like these:

https://hostname/blabla/blabla.xxx

Verry thanks for your help

Laurent
0
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Hi Lolo,
Looking at the original URI I am going to make some assumption that you basically want to strip out the session number? Also the blabla.xxx is the some kind of file where .xxx is the extension. I am also assuming that the LTM is terminating the SSL Connection.

If that is the case the iRule would look something like the following:


 
when HTTP_REQUEST {
if { [HTTP::host] eq "hostname")} {
HTTP::redirect "https://[HTTP::host][URI::path [HTTP::uri] 2][URI::basename [HTTP::uri]]"
}
}


To break this down:
HTTP::host will contain the hostname of the URL
[HTTP::uri] = the entire URI "/(S(session number))/blabla/blabla.xxx"
[URI::path [HTTP::uri] 2] = will look at the path depth at 2. This will result in finding /blabla
[URI::basename [HTTP::uri]] = will look for the basename of the path which is "blabla.xxx"

Here is a article that talks about parsing a URI (Click here)

I hope this helps
#Bhattman
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

Verry thanks for your quick response. All the best

Gotham City is save

Lolo
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Post up your results when you have confirmed whether it has worked or not.

#Bhattman
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Another option for removing the first string between forward slashes is to use scan (Click here):

  
when HTTP_REQUEST {

# Read in everything after the first / that is not a /.
# Save that to $session_id and everything else to $uri_to_save
scan [HTTP::uri] {/%[^/]%s} session_id uri_to_save

# Rewrite the URI without the session ID
HTTP::uri $uri_to_save
}


Here is an example:

% set uri /my_session_id_string/path/to/preserve?query=string
/my_session_id_string/path/to/preserve?query=string

% scan $uri {/%[^/]%s} session_id uri_to_save
2

% puts $uri_to_save
/path/to/preserve?query=string

% puts $session_id
my_session_id_string

Aaron
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
It's OK and i have add filter on file extention:

when HTTP_REQUEST
{
set url [HTTP::uri]
if {[string tolower $url] matches_regex {/\(s\(.*\.(gif|js|css|jpg|bmp)}}
{
scan [HTTP::uri] {/%[^/]%s} session_id uri_to_save
# Rewrite the URI without the session ID
#log local0. "$session_id"
HTTP::uri $uri_to_save
}
}

Thanks all
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
It would be more efficient to use switch and string comparisons to check the object type versus a regex. Here is an example:

 
when HTTP_REQUEST {

# Check the requested path (URI minus query string)
switch -glob [string tolower [HTTP::path]] {
"*.gif" -
"*.js" -
"*.css" -
"*.jpg" -
"*.bmp" {
# Read in everything after the first / that is not a /.
# Save that to $session_id and everything else to $uri_to_save
# scan will return the number of matches
if {[scan [HTTP::uri] {/%[^/]%s} session_id uri_to_save] == 2}{

# Rewrite the URI without the session ID
#log local0. "$session_id"
HTTP::uri $uri_to_save
}
}
}
}


Aaron
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
The last script work verry fine

thanks
0