I am not sure why you want to use the HTTP::respond for achieving this, this can be acheved in a very simple format. Before I go ahead and type the iRule here, for the benefit of the newbies that might stumble across this post here are a few basics
Take a URL of a website, say iam trying to download chrome from Google, My adress bar will show
In this there are 3 parts
1. “http://” --> This is the protocol, other examples that you would see are https or ftp
2. “www.google.com” --> This is hostname of the ser ver you are trying to access (known as host)
3. “/chrome” --> This is URI or Uniform Resource Identifier, it refernces the location inside the server.
This is how we find our way in the websites
To reference all this in iRules, you can use the following tags [HTTP::host] and [HTTP::uri]
Please be informed that “/” is a part of URI, known as the root location. So when you try to access the host without an URI, your browser automatically appends the “/”
So for your case we can try and use the HTTP::redirect feature, as it does the 302 redirection as you want it
Use this iRule
when HTTP_REQUEST {
if {[HTTP::host] equals "host1.domain.com" } {
HTTP::redirect https://securehost.domain.com[HTTP::uri]
}
}
This will redirect your request and append your URI
The 301 vs. a 302 is the request of the client, and to my understanding, HTTP::respond is the only way to issue a 301. Using the example I can append anything to the URI, but I get a double slash in the request
Here is the rule I am using (complements of the devcentral examples)
when HTTP_REQUEST {
Check if there is a class entry which starts with the requested URI
set row [findclass [string tolower [HTTP::host]] $::host_redirects]
Check if there was a matched row
if { $row ne "" }{
log local0. "Matched $row"
Send a response using the status and location from the class
HTTP::respond [getfield $row " " 2] Location [getfield $row " " 3][HTTP::uri] Connection Close
Clear the row variable
unset row
}
}
If the third field in the class don’t have a trailing slash, then you shouldn’t see a double slash in the redirect unless the requested URI had two slashes