Forum Discussion
johns
Employee
Jul 28, 2005Using URI to rewrite host
I am trying to come up with a rule where the first directory name of the uri is used to rewrite the URL. an example will be:
http://www.foo.com/abc/doc/index.html =>
http://abc.foo.com/abc/doc/index.html.
I want to make it so that the rule is general enough to be applied to multiple sites like this. I am thinking I will probably have to use regex to parse up to the second "/" after the URL and store that in a variable, then constrcut URL from that. Is that possible?
Thanks.
25 Replies
- unRuleY_95363Historic F5 AccountTry:
append my_uri "/"
set my_uri "$my_uri/"
- johns
Employee
Thanks for everyone's help. I just can't seem to come up with a clean way to adding a trailing slash to the request if it did not have it AND if it is not a request for a file. - If you can provide the logic as to how something is a request for a file, then we can help you out.
- johns
Employee
Thanks, Joe. - drteeth_127330Historic F5 AccountYou might have to use regular expressions after all. Something like this might work:
if {regexp $my_uri {.+/[^\.]+[^/]$}} { append $my_uri "/" }
- If you have a situation where dots are valid in directory names, or you want to limit file types, you could use an external data group in combination with a matchclass command and ends_with operator to make sure the file is a valid file type
class file_extensions { ".gif" ".jpg" ".htm" ".html" ".jsp" ".asp" } when HTTP_REQUEST { set host [HTTP::host] set uri [HTTP::uri] set token [getfield [HTTP::uri] "/" 2] if { $token eq "" } { log local0. "no path specified so ignore" } else { if { [matchclass $uri ends_with $::file_extensions] > 0 } { log local0. "Found file request, leave as is!" } else { log local0. "Not a file request, update host and check for trailing slash " set host "$token.$host" if { $uri ends_with "/" } { log local0. "Uri ends with a slash, all's good" } else { log local0. "Uri doesn't end with a slash, appending one" append uri "/" } } } update host log local0. "host: $host" log local0. "uri : $uri" HTTP::header replace "Host" $host HTTP::uri $uri }
- johns
Employee
Thanks for the help, Joe. I am stil having problem getting the correct result, though. The rule works in HTTPS with the trailing slash, but when the slash is not used, the Java dropdown menu's do not work, nor any of the buttons. It seems the links that are within the page do not contain the directory path that was initially in the first request. - Without seeing your iRule there isn't much I can do to help out from here. The best bet you have is to throw in a bunch of log commands and debug from there. At that point you can find where your logic is breaking. If you need to modify response content, there are a couple of entries in the forums about modifying the uri on the inbound request and then modifying the content back to the original uri on the outbound request. Check out my blog for a synopsis of the post (Click here)
- johns
Employee
The current rule is:class file_extensions { ".asp" ".aspx" ".gif" ".htm" ".html" ".jpg" ".js" ".jsp" }
rule host_rewrite2 { when HTTP_REQUEST { set host [HTTP::host] log local0. $host set uri [HTTP::uri] set token [getfield [HTTP::uri] "/" 2] if { $token eq "" } { log local0. "no path specified so ignore" } else { if { [matchclass $uri ends_with $::file_extensions] > 0 } { log local0. "Found file request, leave as is!" } else { log local0. "Not a file request, update host and check for trailing slash " set host "$token.$host" if { $uri ends_with "/" } { log local0. "Uri ends with a slash, all's good" } else { log local0. "Uri doesn't end with a slash, appending one" append uri "/" } } } update host log local0. "host: $host" log local0. "uri : $uri" HTTP::header replace "Host" $host HTTP::uri $uri } }
- unRuleY_95363Historic F5 AccountI'm not sure what I can say about the Javascript not working. Perhaps they have hard-coded some absolute uri's in there that are not based on the original requested uri.
rule host_rewrite2 { when HTTP_REQUEST { set host [HTTP::host] log local0. $host set uri [HTTP::uri] set token [getfield [HTTP::uri] "/" 2] if { $token eq "" } { log local0. "no path specified so ignore" } else { if { [matchclass $uri ends_with $::file_extensions] > 0 } { log local0. "Found file request, leave as is!" } else { log local0. "Not a file request, update host and check for trailing slash " if { not ( $host starts_with $token ) } { set host "$token.$host" log local0. "Updated host with token: $host" HTTP::header replace "Host" $host } if { $uri ends_with "/" } { log local0. "Uri ends with a slash, all's good" } else { log local0. "Uri doesn't end with a slash, appending one" append uri "/" HTTP::uri $uri } } } log local0. "host: $host" log local0. "uri : $uri" } }
Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects