Forum Discussion
lguerra07_54491
Nimbostratus
Mar 09, 2010HTTP to HTTPS 301 redirect with datagroups
I simply need to redirect from HTTP to HTTPS and get a 301 code. I was able to get this to work with the following if statement:
--------
when HTTP_REQUEST {
if { [HTTP::host] eq "www.domain.com" and [HTTP::uri] ends_with "/uri1/test.html" }{
HTTP::respond 301 Location "https://www.domain.com/uri1/test.html"
}
}-----------
The big question would be, how can I make this work with a datagroup? this is my datagroup so far:
------
class 301_redirect {
"www.domain.com https://www.domain.com/uri1/test.html"
"www.domain.com https://www.domain.com/uri2/test.html"
}------
any help is appreciated.
thanks.
- hoolio
Cirrostratus
Can you provide more examples of the host and URI strings you want to check and the URLs you want to redirect to? - lguerra07_54491
Nimbostratus
Yes, the host check is always going to be for one domain and then just checking if the URI matches. - hoolio
Cirrostratus
So it looks like you want to send a 301 redirect to https if the host is "www.domain.com" and the URI is listed exactly in the datagroup? If so, you can add the URIs to a string datagroup one per line and then use an iRule to check the datagroup and send a redirect:class my_paths_class { "/uri1/test.html" "/uri2/test.html" "/uri3/test.html" "/uri4/test.html" }
when HTTP_REQUEST { Check if host set to lowercase is www.domain.com if {[string tolower [HTTP::host]] eq "www.domain.com"}{ Check if path is in the datagroup if {[matchclass [HTTP::path] equals my_paths_class]}{ Send a 301 redirect to https HTTP::respond 301 Location "https://www.domain.com[HTTP::uri]" } } }
- lguerra07_54491
Nimbostratus
I tried it and it gave me a page cannot be displayed error when browsing to www.domain.com or to any URI underneath it. - hoolio
Cirrostratus
Which LTM version are you running? - lguerra07_54491
Nimbostratus
Im using version 9.3.1.. - lguerra07_54491
Nimbostratus
Thanks Aaron.. I looked at the log files and found errors about the naming convention of my datagroup... I was using "-" (dashes) in the name and apparently thats a problem. (at least on version 9.3.1). - hoolio
Cirrostratus
Glad you figured it out. You can use hyphens in TCL variables (or datagroup names), but you need reference them with curly braces: ${::domain-https-301-redirect}. It's easier to replace the hyphens with underscores or nothing.
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