Forum Discussion
Misty_Spillers
Feb 28, 2012Nimbostratus
Redirecting 404 to a friendly page by uri
I have done lots of searching and have even come across posts of exactly what I want to do but is doesn't seem to work for me. I'm wondering if I'm running into a problem because I'm running 9.4.4 but it seems simple enough.
Our websites are laid out like this
www.main.com
www.main.com/agency1
www.main.com/agency2
Agency1 is getting a complete makeover and I want to redirect 404's just for them to their own page.
Why doesn't this rule work? (the 404 pages still show)
when HTTP_REQUEST {
set uri [string tolower [HTTP::uri]]
}
when HTTP_RESPONSE {
if { [HTTP::status] == 404 and $uri eq "/agency1"} {
HTTP::redirect "http://www.main.com/agency1/moved.html"
}
}
My only guess is that it never equals "agency1" if I remove the that part it redirect all 404's from www.main.com which makes sense.
Thanks in advance,
Misty
- Wil_Schultz_101NimbostratusI try to stay away from if statements, no good excuse other than switches are sometimes faster.
when HTTP_REQUEST { set path [string tolower [HTTP::path]] } when HTTP_RESPONSE { switch [HTTP::status] { "404" { switch -glob $path { "/agency1" { HTTP::respond 301 \ "Location" "http://www.main.com/agency1/moved.html" \ "Server" "www.main.com" } } } } }
- Misty_SpillersNimbostratusI must be missing something in general because that doesn't work for me either. I obviously changed the 3 sanitized variables in your example.
- Wil_Schultz_101NimbostratusSure, it would seem that your $path isn't hitting.
when HTTP_REQUEST { set path [string tolower [HTTP::path]] } when HTTP_RESPONSE { switch [HTTP::status] { "404" { switch -glob $path { "/agency1" { HTTP::respond 301 \ "Location" "http://www.main.com/agency1/moved.html" \ "Server" "www.main.com" } default { log "We have a miss, the actual path is $path" } } } } }
- hooleylistCirrostratusYou probably want to add a * to the end of /agency1 to match paths starting with /agency1. Without the asterisk, it will only match on the exact /agency1 path.
- Wil_Schultz_101NimbostratusNot if she's redirecting it to "http://www.main.com/agency1/moved.html" :-)
edit: I suppose if it's only triggering on 404's it should be okay with an asterisk.
- Misty_SpillersNimbostratusOk the wildcard on Wil's code seems to do what I wanted it to.
- Misty_SpillersNimbostratusSorry to bug you but I'm thinking ahead and if agency2 finds out we are doing this for agency1, they are going to want it too. What is the most efficient way to do this for agency2 as well? or is just adding another irule with /agency2's info good enough?
- Wil_Schultz_101NimbostratusYou can easily add another clause to the switch.
when HTTP_REQUEST { set path [string tolower [HTTP::path]] } when HTTP_RESPONSE { switch [HTTP::status] { "404" { switch -glob $path { "/agency1*" { HTTP::respond 301 \ "Location" "http://www.main.com/agency1/moved.html" \ "Server" "www.main.com" } "/agency2*" { HTTP::respond 301 \ "Location" "http://www.main.com/agency2/moved.html" \ "Server" "www.main.com" } } } } }
- Misty_SpillersNimbostratusI'm always so nervous posting my newbie questions here and I'm always smiling at the at results, Thanks for all the help. One last question and I'll try to answer it myself. I think you did pretty much answer it in the log example I just want to make sure its the right way to do this. Also if the statement is in the right place.
when HTTP_REQUEST { set path [string tolower [HTTP::path]] } when HTTP_RESPONSE { switch [HTTP::status] { "404" { switch -glob $path { "/agency1*" { HTTP::respond 301 \ "Location" "http://www.main.com/agency1/moved.html" \ "Server" "www.main.com" } "/agency2*" { HTTP::respond 301 \ "Location" "http://www.main.com/agency2/moved.html" \ "Server" "www.main.com" } default { HTTP::respond 301 \ "Location" "http://www.main.com/moved.html" \ "Server" "www.main.com" } } } } }
- Wil_Schultz_101NimbostratusCareful here. Technically, yes... just keep in mind that this will be the default action when somebody is presented with a 404.
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