Forum Discussion
brettbeggs_1177
Nimbostratus
Apr 07, 2010iRule Request - strip sub domains
I'm trying to create an iRule that can be used similar to the following mod rewrite command:
RewriteCond %{HTTP_HOST} ^.+\.(.+)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://%1.mydomain.com$1 [R=301,L]
What we're wanting is anything that hits https://*.sub.mydomain.com to be redirected to https://sub.mydomain.com. I guess i'm just not sure how to strip everything before sub.mydomain.com from the URL while maintaining whatever is in that first sub domain spot.
Ideally this single rule would work for any of these variants:
https://*.sub1.mydomain.com > https://sub1.mydomain.com
https://*.sub2.mydomain.com > https://sub2.mydomain.com
https://*.whatever.mydomain.com > https://whatever.mydomain.com
If it matters, the virtual server is configured with a wildcard ssl certificate *.mydomain.com so we're trying to avoid customers instinctively adding www. to the beginning of the url and getting a certificate error.
Thanks for helping!
- hoolio
Cirrostratus
Hi Brett,when HTTP_REQUEST { log local0. "[IP::client_addr]:[TCP::client_port]: [HTTP::method] request to [HTTP::host][HTTP::uri]" Split the host header value on periods into a list set host_list [split [HTTP::host] "."] log local0. "[IP::client_addr]:[TCP::client_port]: Parsed host list $host_list" Check if there were more than three fields if {[llength $host_list] > 3}{ Send a redirect with the third to last field from the requested host prepended to .mydomain.com HTTP::respond 301 Location "https://[lindex $host_list [expr {[llength $host_list] -3}]].mydomain.com" log local0. "[IP::client_addr]:[TCP::client_port]: Redirecting to\ https://[lindex $host_list [expr {[llength $host_list] -3}]].mydomain.com" } }
- brettbeggs_1177
Nimbostratus
Thanks Aaron.Send a redirect with the third to last field from the requested host prepended to .mydomain.com HTTP::respond 301 Location "https://[lindex $host_list [expr {[llength $host_list] -3}]].mydomain.com"
- hoolio
Cirrostratus
Thanks for the correction on the redirect location protocol. I assumed you were using this rule on an HTTP VIP. As you found it's too late to fix this when the client is making an HTTPS request with a hostname that doesn't match the SSL cert. For HTTPS, LTM needs to decrypt the SSL before viewing the HTTP or sending an HTTP response. In order to decrypt the SSL, LTM needs to send its cert and complete an SSL handshake. - brettbeggs_1177
Nimbostratus
This is so frustrating... - hoolio
Cirrostratus
What do the logs show (/var/log/ltm)? Is the HTTPS VIP redirecting some or all requests to HTTP? - brettbeggs_1177
Nimbostratus
Ahh, yes...i was stupid and had the same irule on the http and https virtual server. I tweaked the version running on the https VS and now all is well. Thanks for all your help.
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