Forum Discussion

Misty_Spillers's avatar
Misty_Spillers
Icon for Nimbostratus rankNimbostratus
Feb 28, 2012

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

15 Replies

  • I'm sorry one more question. What if it was a vanity domain (controlled by host header) on the same VIP that I refer to above. would it work too? something like

    
    when HTTP_REQUEST {
     
          set host [string tolower [HTTP::host]]
    
          }
    when HTTP_RESPONSE {
        switch [HTTP::status] {
      
       "404" {
       switch -glob $host {
        
       "*.vanitydomain.com/*" { 
         
       HTTP::respond 301 \
         
       "Location" "http://www.vanitydomain.com/moved.html" \
         
       "Server" "www.vanitydomain.com"
        
    } 
       } 
      } 
     } 
    }

    I'm not sure if I'm even close on this nor how to stick it in the other irule.

    I again really appreciate the help, I'm learning a lot.

  • So to keep it dynamic, you could grab the host header on HTTP_RESPONSE, and use that variable later on down the line.

    
    when HTTP_REQUEST {
     set host [string tolower [HTTP::host]]
     set path [string tolower [HTTP::path]]
    }
    
    when HTTP_RESPONSE {
    switch [HTTP::status] {
      "404" {
       switch -glob $path {
        "/agency1*" { 
         HTTP::respond 301 \
         "Location" "http://$host/agency1/moved.html" \
         "Server" "$host"
        }
        "/agency2*" { 
         HTTP::respond 301 \
         "Location" "http://$host/agency2/moved.html" \
         "Server" "$host"
        }
       } 
      } 
     } 
    }
     
  • Thanks for helping me under stand this. You are helping me with 2 issues now. I tried a variety of ways to make /agency2 a vanity domain but not having much luck Could I just get one more sample leaving /agency1 the way it is, agency2 is now www.agency2.com?

     

     

    my other issue? yeah is it an iRule with 88 "if" statements in it. I'm hoping I can apply what I'm learning here to that.

     

     

    Thanks again!
  • The above iRule is completely agnostic in regards to hostname, the redirect is to the same hostname that was originally in the host header.

     

     

    If you're adding to an existing iRule you might want to create a test VIP and use curl or wget to make sure you're getting the expected responses.

     

  • oh I completely missed how you added the "$host" to the second part. That make sense except that I wouldn't be matching /agency2 anymore. It would need to be a condition of 404 + host so I thought I would have to do another "switch"

     

     

    I could just keep the vanity domains separate or just deal with them when someone requests it

     

     

    I do have a test vip that mirrors production, I'm not going to add this to my huge redirect irule, just going to keep it separate. It seems to be happy that way. I'll save my "whats the best way to handle a rule with 88 if statements in it" for another discussion as I have taken too much of your time already :) Thanks a bunch