Forum Discussion
slackwaresuppor
Nimbostratus
May 28, 2008stripping data
how would i strip the www. off of www.domain.com??
10 Replies
- JRahm
Admin
Like this:[getfield [HTTP::host] ".domain.com" 2] - slackwaresuppor
Nimbostratus
when HTTP_REQUEST {
HTTP::redirect https://www.domain1.com/nsa/+homePage=[getfield [HTTP::host] ".domain.com" 2]
}
that correct?? bc it does not work.... i just get
https://www.domain1.com/nsa/+homePage= - slackwaresuppor
Nimbostratus
also would like to try and put in an if statement, if it has www, strip it off, if not, just forward with the domainname. - slackwaresuppor
Nimbostratus
so this is what ive come up with..
when HTTP_REQUEST {
if {[getfield [HTTP::host] ".domain.com" 1] eq "www" }
{HTTP::redirect https://www.domain1.com/nsa/+homePage=[getfield [HTTP::host] ".domain.com" 2]
}
else {HTTP::redirect https://www.domain1.com/nsa/+homePage=[HTTP::host]}
}
the non www is working.. the www is not.. any ideas? - slackwaresuppor
Nimbostratus
also.. the domain.com could be anything not just a single domain, so i need to check all incoming www.?
not just www.domain.com - slackwaresuppor
Nimbostratus
ahhhhh...
SOLUTION:
when HTTP_REQUEST {
if {[getfield [HTTP::host] "." 1] starts_with "www" }
{HTTP::redirect https://www.domain.com/nsa/+homePage=[getfield [HTTP::host] "." 2].[getfield [HTTP::host] "." 3]}
else {HTTP::redirect https://www.domain.com/nsa/+homePage=[HTTP::host]}
} - In this example you are doing the getfield twice which is causing double string allocations. If all you want to do is check whether a "www." is prefixing your domain, you can do so with the "starts_with" operator. And then, since you know you want to strip off the first 4 characters, you can use the "string range" command to do that.
This code will set a variable to the value of the requested HTTP::host. If that value starts with a "www.", it will strip the "www." off and assign that to the variable. It will then issue a redirect with either the original or stripped host value.when HTTP_REQUEST { set host [HTTP::host] if { [HTTP::host] starts_with "www." } { set host [string range [HTTP::host] 4 end] } HTTP::redirect "https://www.domain1.com/nsa/+homePage=$host" }
Is this what you are going for?
Oh, BTW, make sure you don't have this iRule on your www.domain1.com virtual or you'll get yourself into an infinite loop.
-Joe - John_Alam_45640Historic F5 AccountSince it is fun we are after, how is this:
when HTTP_REQUEST {
HTTP::redirect "https://www.domain1.com/nsa/+homePage="+[regsub {^www\.} [HTTP::host] ""]
}
If i got all the synthax correct, regsub should remove www. if it finds it, if it does not, it should return [HTTP::host} as is.
John - Deb_Allen_18Historic F5 AccountOuch, please let's not go with regsub to solve such a simple problem. Very pretty all on one line & everything, but very expensive command to invoke regardless how simple the operation itself might be.
Why not simply use the "domain" command:
which will return only the last 2 parts of any domain name.when HTTP_REQUEST { HTTP::redirect "https://www.domain1.com/nsa/+homePage=[domain [HTTP::host] 2]" }
If you truly need to operate only on those domains with www. prefix, then use the starts_with operator to condition the change as in colin's example:when HTTP_REQUEST { if { [HTTP::host] starts_with "www." } { HTTP::redirect "https://www.domain1.com/nsa/+homePage=[domain [HTTP::host] 2]" } else { HTTP::redirect "https://www.domain1.com/nsa/+homePage=[HTTP::host]" } }
/deb - Deb_Allen_18Historic F5 Accountoh, that was ugly.
re-submitted, hopefully you can read it now...
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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