Forum Discussion
johns
Jul 28, 2005Ret. Employee
Using 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 "/"
or even:set my_uri "$my_uri/" - johnsRet. EmployeeThanks 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.
If you are specifically looking for dots in the request, you could use the "contains" operator or if you know it will end with a certain extension, you could use the "ends_with" operator as well. There are lots of requests that can return "files" but don't have specific file extensions but that will depend on your application. There are also valid requests that contain dots that are not requests for files...
Knowing your requirements specifically will help building the rule.
-Joe - johnsRet. EmployeeThanks, Joe.
What I am trying to do:
Request from user => BIG-IP rule rewrites
www.site.com/dir1 => dir1.www.site.com/dir1/
www.site.com/dir1/file.htm => www.site.com/dir1/file.htm
www.site.com/dir2/ => dir2.www.site.com/dir2/ able to get this done
Basically, the server needs to receive different host header for each dir path, and if it does not end with slash and it indicates a directory name, I need to append a slash. - drteeth_127330Historic F5 AccountYou might have to use regular expressions after all. Something like this might work:
if {regexp $my_uri {.+/[^\.]+[^/]$}} { append $my_uri "/" }
This regex matches if the last path component does not end with a slash and it does not contain a period followed by at least one character. - 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 }
*Note, the class is an external string class created in the GUI and is not part of the actual rule code.
This could use some cleaning up a bit and testing...
-Joe - johnsRet. EmployeeThanks 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.
The rule did add the path twice in front if the original host header, so I ended up commenting out the "HTTP::header replace "Host" $host" from the bottom of the rule.
And if I try the same in the HTTP, it simply does not work, and I have yet to figure out what is causing it.
I thought perhaps support would help, but they pointed me back here as the best resource. Thanks again for your help.
John - 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)
-Joe - johnsRet. EmployeeThe 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.
As for the host, you could add a check to see if the token is already there and not add it if it is. Also, I'd move the setting of the uri/host into the if body so you are only changing it when it needs to be changed.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" } }
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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