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...
Aug 25, 2005
One more try. I've tried to simplify the logic in your code and based off of your requirements:
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/
this code should work:
when HTTP_REQUEST {
set host [HTTP::host]
set uri [HTTP::uri]
set token [getfield [HTTP::uri] "/" 2]
If no uri is passed in, then leave everything as is
if { $token ne "" } {
if { [matchclass $uri ends_with $::file_extensions] > 0 } {
for case www.site.com/dir1/file.htm => www.site.com/dir1/file.htm
log local0. "Found file request, leaving host and uri alone"
} else {
log local0. "Not a file request, update host and check for trailing slash "
Check if host starts with token
If not, then prepend it with the token
if { ! ($host starts_with $token) } {
ie. www.site.com/dir1 => dir1.www.site.com/dir1
log local0. "Host doesn't start with token so updating host"
set host "$token.$host"
HTTP::header replace "Host" $host
}
Check if url ends with a slash
if not, then append it with one
if { ! ($uri ends_with "/") } {
ie. dir1.www.site.com/dir1 => dir1.www.site.com/dir1/
log local0. "Uri doesn't end with a slash, updating"
append uri "/"
HTTP::uri $uri
}
}
}
log local0. "host: $host"
log local0. "uri : $uri"
log local0. "full: $host$uri"
}
-Joe
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