Forum Discussion
DaveC_53879
Nimbostratus
Jul 26, 2011Return a 301 instead of 302
I need my redirects to return a 301 instead if a 302. The Web team is unable to track the rdirected requests because they return a 302. How can I change the rule below to return a 301?
when HTTP_REQUEST { if { [HTTP::host] starts_with "www."} {
HTTP::redirect ]
} elseif { [HTTP::host] starts_with "mydomain.com"} {
HTTP::redirect ]
There are more possible redirects in the irule which I've omitted, but they all use the same format. Thanks...
13 Replies
- The_Bhattman
Nimbostratus
Hi Dave,
Here is an example using the switch command.when HTTP_REQUEST{ switch -glob [string tolower [HTTP::host]] { "www.*" { HTTP::respond 301 "http://mydomain.org[HTTP::uri]" } "mydomain.com*" { HTTP::respond 301 "http://mydomain2.org[HTTP::uri]" } } }
I hope this helps
Bhattman - DaveC_53879
Nimbostratus
Thanks Bhattman. When I try to add your text just as you have it I get the message below. Am I missing some syntax?
01070151:3: Rule [mydomain.org_301-redirect] error:
line 1: [wrong args] [when HTTP_REQUEST{]
line 2: [command is not valid in the current scope] [switch -glob [string tolower [HTTP::host]] {
"www.*" { HTTP::respond 301 "http://mydomain.org[HTTP::uri]" }
"mydomain.com*" { HTTP::respond 301 "http://mydomain2.org[HTTP::uri]" }
}]
line 2: [command is not valid in the current scope] [string tolower [HTTP::host]]
line 2: [command is not valid in the current scope] [HTTP::host]
line 6: [command is not valid in the current scope] [}]
Thanks,
Dave - Ryan_Paras_7933
Nimbostratus
BTW...
Have you looked at using something along the lines on this to look for 302 and then deal with it then?
when HTTP_RESPONSE {
if { [HTTP::status] contains "302"} {
...............
}
} - DaveC_53879
Nimbostratus
OK. I figured out the syntax; a missing space. But my rule doesn't work. It just returns a blank page. Sanitized rule below, so there may be some duplicates.
when HTTP_REQUEST {
switch -glob [string tolower [HTTP::host]] {
"www.*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"peoples.net*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"peoples.com*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"peoples.mobi*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"peoplesfor.org*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"peoplesfor.net*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"peoplesfor.com*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"speople.net*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"speople.org*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"speople.com*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"people.net*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"people.com*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"-people.org*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"-people.net*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"-people.com*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"-peoples.org*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"-peoples.net*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"-peoples.com*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
"people.org*" { HTTP::respond 301 "http://peoples.org[HTTP::uri]" }
}
} - DaveC_53879
Nimbostratus
Ryan,
How would I use that since my redirects are generating the 302?
Thanks - Ryan_Paras_7933
Nimbostratus
I am sorry - I think I misunderstood. I thought the issue was your web servers were sending 302's and you wanted to change them to 301's. Please disregard my comments. - DaveC_53879
Nimbostratus
NP. I appreciate the response. - nitass
Employee
But my rule doesn't work. It just returns a blank page.
have u ever tried curl command e.g. curl -i http://www.people.org/?
if so, what response did u get? - hoolio
Cirrostratus
HTTP::respond will not automatically set the URL in the Location header like HTTP::redirect will. So you need to set the URL as the Location header value. Also, with switch you can use - as an action if it's the same action for multiple cases. Lastly, can you double check your * wildcards to make sure you're using it to match in the correct location. The HTTP::host value will only return www.example.com if the URL is http://www.example.com/some/path.html.when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "www.*" - "peoples.net" - "peoples.com" - "peoples.mobi" - "peoplesfor.org" - "peoplesfor.net" - "peoplesfor.com" - "speople.net*" - "speople.org" - "speople.com" - "people.net" - "people.com" - "*-people.org" - "*-people.net" - "*-people.com" - "*-peoples.org" - "*-peoples.net" - "*-peoples.com" - "people.org" { HTTP::respond 301 Location "http://peoples.org[HTTP::uri]" } } }
Aaron - DaveC_53879
Nimbostratus
Thanks Aaron. Do I need the wildcards at all since I'm matching the host name exactly?
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
