Forum Discussion
John_Merritt_45
Nimbostratus
Nov 06, 2006Need help with multi domain url rewrite
I am new to the iRule world. I have multiple domains that I need to rewrite. The first group should all rewrite to the same url. However I have another domain that I want to rewrite to a second url.
Not sure where my syntax error is or if there is a better way to do this.
when HTTP_REQUEST {
if { switch [HTTP::host] {
"www.abc.com" -
"www.def.com" -
"www.ghik.com" -
"www.lmnop.com" - {
HTTP::redirect "http://www.xzy.com/content.cfm?pageid=11049]"
}
} else {
HTTP::host equals "order.ac,e.com" {
HTTP::redirect "https://catalog.acme.com"
}
}
}
- Colin_Walker_12Historic F5 AccountYou've got the right idea, but you can combine that logic into one switch statement.
when HTTP_REQUEST { switch -glob [string tolower [HTTP::host] ] { "*abc.com" - "*def.com" - "*ghik.com" - "*lmnop.com" { HTTP::redirect "http://www.xzy.com/content.cfm?pageid=11049]" } "order.ace.com" { HTTP::redirect "https://catalog.acme.com" } } }
- John_Merritt_45
Nimbostratus
Thanks! - Darren_Person_2
Nimbostratus
Hi Colin, - The -glob will give you "globbing" functionality. Not quite regular expressions, but with wildcarding.
class domain_check { "www.abc.com http://www.xzy.com/content.cfm?pageid=11049]" "www.def.com http://www.xzy.com/content.cfm?pageid=11049]" "www.ghik.com http://www.xzy.com/content.cfm?pageid=11049]" "www.lmnop.com http://www.xzy.com/content.cfm?pageid=11049]" "order.ace.com https://catalog.acme.com" } --- Begin iRule --- when HTTP_REQUEST { set redir [findclass [string tolower [HTTP::host]] $::domain_check " "] if { $redir ne "" } { HTTP::redirect "$redir" } }
class domain_one { "www.abc.com" "www.def.com" "www.ghik.com" "www.lmnop.com" } class domain_two { "order.ace.com " } --- Begin iRule --- when HTTP_REQUEST { if { [matchclass [string tolower [HTTP::host]] ends_with $::domain_one] } { HTTP::redirect "http://www.xzy.com/content.cfm?pageid=11049]" } elseif { [matchclass [string tolower [HTTP::host]] ends_with $::domain_two] } { HTTP::redirect "https://catalog.acme.com" } }
class domain_check { "www.abc.com 1" "www.def.com 1" "www.ghik.com 1" "www.lmnop.com 1" "order.ace.com 2" } class redirect_urls { "1 http://www.xzy.com/content.cfm?pageid=11049]" "2 https://catalog.acme.com" } --- Begin iRule --- when HTTP_REQUEST { set redir_idx [findclass [string tolower [HTTP::host]] $::domain_check " "] if { $redir_idx ne "" } { set redir [findclass $redir_idx $::redirect_urls " "] if { $redir ne "" } { HTTP::redirect "$redir" } } }
- Colin_Walker_12Historic F5 AccountTo create the same type of functionality with classes, assuming you have at least 2 domains that you want redirected to each location, you'd likely want to use two seperate classes.
class redirect_to_abc { "host1.com" "host2.net" "host3.biz" } class redirect_to_xyz { "host10.com" "host9.us" "host8.org" }
when HTTP_REQUEST { if { [matchclass [HTTP::host] ends_with $::redirect_to_abc ] } { HTTP::redirect http://www.abc.com } elseif { [matchclass [HTTP::host] ends_with $::redirect_to_xyz] } { HTTP::redirect http://www.xyz.com } else { pool fallback_pool } }
- steve_cross_650
Nimbostratus
I was trying to do something similar to what Joe recommended but unfortunately it does not seem to be working. I am basically trying to automatically redirect to an https page when a user goes to a specific page:class secure_pages { "\page1.aspx" "\page2.aspx" "\page3.aspx" "\page4.aspx" } when HTTP_REQUEST { if { [string tolower [HTTP::host]] equals "test.mywebsite.com"} { if { [matchclass [string tolower [HTTP::uri]] starts_with $::secure_pages]} { HTTP::redirect "https://[HTTP::host][HTTP::uri]" } else { pool pool_A } } else { pool pool_B } }
- The class section is not intended to be part of the iRule contents. It is a String Data Group. The class { ... } format is how it is stored in the bigip.conf configuration file. Remove the class section from the iRule and create a string data group titled secure_pages and your pages and you should be set.
- steve_cross_650
Nimbostratus
Thanks Joe, this code now correctly redirects the pages listed in my string data group "secure_pages" to a https URL:when HTTP_REQUEST { if { [string tolower [HTTP::host]] equals "test.mywebsite.com"} { if { [matchclass [string tolower [HTTP::uri]] starts_with $::secure_pages]} { HTTP::redirect "https://[HTTP::host][HTTP::uri]" } else { pool pool_A } } else { pool pool_B } }
- steve_cross_650
Nimbostratus
Or please let me know if there is a more efficient way of doing this.
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