Forum Discussion

Mike_86382's avatar
Mike_86382
Icon for Altostratus rankAltostratus
Apr 23, 2009

A twist on the 404 status code

Hello. I am new to iRules, but I have an important task to accomplish:

 

 

I need to rewrite an http response status code from 200 to 404 whenever a particular page is sent to the client. We need to do this as we have problems with search engines and 404 page not returning a proper 404.

 

 

So, http://www.mydomain.com/404.htm (which normally returns a 200 because of our CMS system) needs to return a 404 status code whenever this page is served.

 

 

Can this be done with iRules?

 

 

Your help is greatly appreciated.

 

 

Thanks,

 

Mike

 

 

10 Replies

  • This is what I have so far:

     

     

    when HTTP_RESPONSE {

     

    if { [HTTP::header location] contains "http://www.mydomain.com/404.htm" }

     

    HTTP::header replace "Status" "404"

     

    }

     

    }
  • you may try HTTP::respond command

     

     

    http://devcentral.f5.com/Wiki/default.aspx/iRules.HTTP__respond
  • Hi Mike,

     

     

    It sounded like you wanted to change the 200 status to a 400. But your test rule is trying to rewrite a redirect (which would be a 30x status) to a 404. Can you use a browser plugin like HttpFox for Firefox or Fiddler for IE to determine exactly what the application is sending back and then describe what you want to do with it?

     

     

    Unfortunately, HTTP::status can only be used to get (not set) the status of the response. So to "change" the status you could need to send your own response using HTTP::respond. It's fairly simple if you can send your own response content. Or if you need to leave the response data from the server in tact, you'd need to collect the payload for the specific responses and then use HTTP::respond to send a 404 response with the original payload. You can check the HTTP::collect wiki page (Click here) for examples.

     

     

    Aaron
  • HTTP::respond is what you are looking for, mentioned above. You will also need to include a 404 page, by setting it in a variable possibly at RULE_INIT, if it's static.

     

     

    Regards,
  • Thanks for the great direction everyone. To solve the immediate need, I will place our 404 html content in the iRule like so:

     

     

    when HTTP_REQUEST {

     

    if { [HTTP::uri] contains "404.htm" } {

     

    HTTP::respond 404 content {

     

     

     

    Custom 404

     

     

     

    All this content comes from our real 404.htm page, but is manually copied into here.

     

     

     

    }

     

    }

     

    }

     

     

    This is just the first step. I want to try to implement the HTTP::collect and do this the right way as I learn more about iRules. Once again, thanks for helping!

     

     

    -Mike
  • Hi Mike,

     

     

    I'm not sure how a search engine will react if the app redirects with a 302 to a page which the app sends a 200 with content indicating the page couldn't be found. You might want to catch the original 302 if the location contains the custom "404" page and respond with an actual 404 status.

     

     

    Aaron
  • Excellent point Aaron. We will have to change the way this works, because it is issueing a 302 before the 404 (defeats the original purpose). I think we will modify the iRule to detect the server response containing status 302 and the uri of the 404 page, and instead respond with a 404 status and the 404 content.

     

     

    Good catch. Thank you.

     

     

    Mike
  • Something like this should work for this:

     
     when HTTP_RESPONSE { 
      
         Check if response is a redirect and the Location header contains the 404 page 
        if { [HTTP::is_redirect] and [HTTP::header Location] contains "http://www.mydomain.com/404.htm"}{ 
      
            Send a 404 with custom content 
           HTTP::respond 404 content { 
               
               
              Custom 404 
               
               
              All this content comes from our real 404.htm page, but is manually copied into here. 
               
               
           }  
        } 
     } 
     

    Aaron
  • This looks good. I'll modify our iRule and do some testing. I think this is really close to what we need.

     

     

    Thank you.

     

    Mike