Forum Discussion
Replace part of HTTP::host and permanently redirect
I'm looking at redirecting various hosts to a new domain.
Example 1: app.olddomain.com to app.newdomain.com Example 2: anotherapp.olddomain.com to anotherapp.newdomain.com
The problem I'm having (because I'm a newb with string manipulation) is switching that "olddomain" to the "newdomain".
I could do the following for each of the applications but it would mean a huge iRule:
when HTTP_REQUEST {
if { [string tolower [HTTP::host]] equals "app.olddomain.com" } {
HTTP::respond 301 Location "https://app.newdomain.com[HTTP::uri]"
}
}
I was hoping to do something like this:
when HTTP_REQUEST {
if {[HTTP::host] ends_with ".olddomain.com"} {
HTTP::respond 301 Location "https://--part of original host--.newdomain.com[HTTP::uri]"
}
}
Is my idea sound? How do we accomplish the replace?
Thanks!
Either (this is probably more efficient);
when HTTP_REQUEST { if {[HTTP::host] ends_with ".olddomain.com"} { HTTP::respond 301 Location "https://[getfield [HTTP::host] . 1].newdomain.com[HTTP::uri]" } }
Or;-
when HTTP_REQUEST { if {[HTTP::host] ends_with ".olddomain.com"} { HTTP::respond 301 Location "https://[string map {olddomain newdomain} [HTTP::host]][HTTP::uri]" } }
- giltjrNimbostratus
Not 100% sure but something like the following may work:
regsub -all ".olddomain.com" [HTTP::host] ".newdomain.com" newhost HTTP::respond 301 Location "https://$newhost[HTTP::uri]"
- IheartF5_45022Nacreous
Either (this is probably more efficient);
when HTTP_REQUEST { if {[HTTP::host] ends_with ".olddomain.com"} { HTTP::respond 301 Location "https://[getfield [HTTP::host] . 1].newdomain.com[HTTP::uri]" } }
Or;-
when HTTP_REQUEST { if {[HTTP::host] ends_with ".olddomain.com"} { HTTP::respond 301 Location "https://[string map {olddomain newdomain} [HTTP::host]][HTTP::uri]" } }
- Martin_Thomas_1Nimbostratus
Awesome! Thanks!!
This one fits the bill nicely and is efficient:
when HTTP_REQUEST { if {[HTTP::host] ends_with ".olddomain.com"} { HTTP::respond 301 Location "https://[string map {olddomain newdomain} [HTTP::host]][HTTP::uri]" } }
Recent Discussions
Related Content
* 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