Rewrite the content in URI with a short URL

Hi Experts

I need assistance on shortening of URL as follows

If a web page with following Source URL is browsed , then a part of URI (highlighted in bold) should be converted to “en” (which is English language ) and output should be Masked URL as shown below .

Source URL : http://hostname/**secureapp/connect/report\_en/REP**/XYZ

Masked URL : http://hostname/**en**/XYZ

Regards, Ankur

Do you want this masked URL to actually work when you click it? Or you just want to mask it.

If you simply want to mask URL’s then apply a STREAM profile with the following iRule.

when HTTP_RESPONSE {
  log local0. "...in HTTP_RESPONSE"
  set search ""
  foreach pair [class get myclass] { 
    set search "$search@[lindex $pair 0]@[lindex $pair 1]@"
  }
  log local0. "search=$search"
  STREAM::expression $search
  STREAM::enable
}

Then create a string data class myclass as follows…

Name                               Value
/secureapp/connect/report_en/REP   /en
/peter                             /pan

And get the following output from a logs

Rule /Common/myrule : ...in HTTP_RESPONSE
Rule /Common/myrule : search=@/secureapp/connect/report_en/REP@/en@@/peter@/pan@

And the resulting web page…

It works!

/en
/pan

Please see update version below

I am unable to update my original post. Here is an updated version.

If you simply want to mask URL’s then apply a STREAM profile with the following iRule.

when HTTP_REQUEST {
  log local0. "..in HTTP_REQUEST"

   no replacement for requests
  STREAM::disable
   disable compression server side
  HTTP::header remove "Accept-Encoding"
}
when HTTP_RESPONSE {
  log local0. "...in HTTP_RESPONSE"

   only replace text content
  if {!([HTTP::header value Content-Type] contains "text")}{ return}

  set search ""
  foreach pair [class get myclass] { 
    set search "$search@[lindex $pair 0]@[lindex $pair 1]@"
  }
  log local0. "String replace=$search"
  STREAM::expression $search
  STREAM::enable
}

Then create a string data class myclass as follows…

Name                                      Value
/secureapp/connect/report_en/REP   /en
/peter                             /pan

And get the following output from a logs

Rule /Common/myrule : ...in HTTP_REQUEST
Rule /Common/myrule : ...in HTTP_RESPONSE
Rule /Common/myrule : search=@/secureapp/connect/report_en/REP@/en@@/peter@/pan@

And the resulting web page…

It works!

/en
/pan

Hi Kevin,

Thanks for the response !! Though i am ok working with irules , however the above example is little tough for me to understand . Could you please explain me the logic behind the same . Also request to consider the below URLs in your example

Source URL : http://www.myweb.com/secureapp/connect/report\_en/REP/XYZ

Masked URL : http://www.myweb.com/en/XYZ

Regards, Ankur

The STREAM commands allow search and replace of data passing to and from the webserver. In this case I have enabled it only on the response coming back from the webserver… (STREAM::enable/disable). The stream expression is a sequence of search and replace pairs in the format @search@replace@. The foreach loop builds a stream expression from name and value pairs in the class myclass. We also turn off compression as most websites send data compressed although the user is never aware of this but we cannot process compressed data. Removing the Accept-Encoding header means the website will not compress the reply.

So any webpage with http://www.myweb.com/secureapp/connect/report\_en/REP/XYZ on it will display http://www.myweb.com/en/XYZ instead.

Is this what you want? Or is it when they click on http://www.myweb.com/en/XYZ they get the webpage at http://www.myweb.com/secureapp/connect/report_en/REP/XYZ instead?

If thats what you are after then thats rather simple.

when HTTP_REQUEST {
  log local0. "..in HTTP_REQUEST"

  if {[class match [HTTP::uri] equals myuri]} {
    HTTP::uri [class match -value [HTTP::uri] equals myuri]
  }
}

Using class myuri

Name      Value
/en       /secureapp/connect/report_en/REP/XYZ

Hi Kevin

Thanks for the detailed explanation !! Yes , the previous one is correct . I want any webpage with http://www.myweb.com/secureapp/connect/report\_en/REP/XYZ to be displayed as http://www.myweb.com/en/XYZ in the users’ browser . Also please note that whenever the URI is triggered in any other part of the web page (any other link present inside the webpage containing this URI ) , then also it should replace /secureapp/connect/report_en/REP/ with /en . I believe that iRule configuration you mentioned above would do that as well.

Also , do i need to simply apply this iRule to the Virtual Server OR apart from applying this iRule to VS , i have to create a separate Stream profile as well and then apply to the VS . I have not worked on Stream profile before and would like to know what needs to be filled in Source and Target part ?

Regards, Ankur

The stream profile by default works in both directions. Only through iRules can you control when it is applied. I you have seen in the first example I do this.

You apply the iRule to the resources section of your virtual server and you must add a stream profile to have access to the STREAM iRule commands. You can use the system default stream profile and you do not need to fill out any fields in the profile itself. The STREAM::expression command in the iRule does that for you. You add the profile in the advanced view of your virtual server properties. Note the iRule will not apply unless there is a stream profile attached.

In the data class I specified name and value. Name is your Search and and value is what it will be replaced with.

Hi Kevin,

Thanks a lot !! I will add “system default stream profile” to VS and will add the said irule for the VS . Is there any way to test out via F5 CLI if it is working ?

Regards, Ankur

If any of the above posts have provided a solution to your issue, please indicate so by clicking the tick to the left of them. This gives feedback and recognition to the volunteers who responded to your issue.

I just setup a webpage with the strings on it and called that webpage through a virtual with the iRule attached. There is no way to trigger logging event when STREAM actually replaces something.

Hi Kevin

One more request , i would like to get the above iRule triggered only when end user browses www.myweb.com . The reason is that there are multiple websites hosted on backend physical servers (residing behind F5) ; however those websites are represented by F5 Virtual IP address.

Regards , Ankur

when HTTP_REQUEST {
  log local0. "..in HTTP_REQUEST"
  set host [string tolower [HTTP::host]]
  if {$host ne "www.myweb.com"} { return }
   no replacement for requests
  STREAM::disable
   disable compression server side
  HTTP::header remove "Accept-Encoding"
}
when HTTP_RESPONSE {
  log local0. "...in HTTP_RESPONSE"
  if {$host ne "www.myweb.com"} { return }
   only replace text content
  if {!([HTTP::header value Content-Type] contains "text")}{ return}

  set search ""
  foreach pair [class get myclass] { 
    set search "$search@[lindex $pair 0]@[lindex $pair 1]@"
  }
  log local0. "String replace=$search"
  STREAM::expression $search
  STREAM::enable
}

Hi Kevin

The above is not working . I configured the above iRule and assigned it to VS along with enabling stream. I have built a dummy URL on the backened webserver and when i browse this URL , the URL in the address bar remains unchanged ]

Regards,

Ankur

I think you need a more comprehensive solution. Have a look at DevCentral

Hi Kevin,

The URL recommended by you contains the solutions which are difficult to understand and which doesnt mention the shortening of URIs . Is there any other way of doing it ?

Regards,Ankur

The above is not working . I configured the above iRule and assigned it to VS along with enabling stream. I have built a dummy URL on the backened webserver and when i browse this URL , the URL in the address bar remains unchanged

i understand Kevin’s irule does replace string (e.g. uri) in response from server. it does not affect if you browse the uri directly.

Source URL : http://www.myweb.com/secureapp/connect/report\_en/REP/XYZ

Masked URL : http://www.myweb.com/en/XYZ

do you want when browsing http://www.myweb.com/secureapp/connect/report\_en/REP/XYZ, url in address bar is changed to http://www.myweb.com/en/XYZ but uri to server is still /secureapp/connect/report_en/REP/XYZ?

or does user have to browse http://www.myweb.com/en/XYZ (http://www.myweb.com/secureapp/connect/report\_en/REP/XYZ is no not available at all) but bigip changes uri when sending to server to /secureapp/connect/report_en/REP/XYZ?

Hi nitass

Yes , you understood it correctly . My requirement is first one you mentioned in above statement .

"when browsing http://www.myweb.com/secureapp/connect/report\_en/REP/XYZ, url in address bar is changed to http://www.myweb.com/en/XYZ but uri to server is still /secureapp/connect/report_en/REP/XYZ "

Regards, Ankur