Forum Discussion

Brett__01_13258's avatar
Brett__01_13258
Icon for Nimbostratus rankNimbostratus
Feb 10, 2014

404 redirect exception

Hi I hope someone can help with this

I have a 404 redirect rule that checks on the primary pool first and if it cant find check in another location, if it cant find it on the second server it sends to a search results page on the primary pool via another irule.

Works well, but the problem is the client gets a 200 from the search results so any page that is not found gets crawled.

I want to be able to put a 404 on the search results page, so it wont be crawled, but would not be caught up in irule the loop.

I have tried below it is getting caught up:

when HTTP_REQUEST { set uri [string tolower [HTTP::path]] }

when HTTP_RESPONSE { if { [HTTP::status] == 404 && $uri ne "/search-results/?p=nothere"} { HTTP::redirect "http://secondserver.location$uri" } }

Has anyone been able to do this?  

            Cheers Brett

1 Reply

  • If I understand you correctly, you want requests to the search results page to return a 404 (and the search results content), when it normally returns a 200. If so, the following should work (modify as required):

    when HTTP_REQUEST {
        if { [HTTP::uri] equals "/" } {
            set rewrite_status 1
        }
    }
    when SERVER_CONNECTED {
        if { [info exists rewrite_status] } {
            unset rewrite_status
            TCP::collect
        }
    }
    when SERVER_DATA {
        if { [regsub -all -nocase "200 OK" [TCP::payload] "404 Not Found" newdata] } {
            TCP::payload replace 0 [TCP::payload length] $newdata
        }
        TCP::release
    }
    

    The basic idea is that since the HTTP::status command is read only, you need to write to the (TCP) data below layer 7. So using the SERVER_CONNECTED command and then a subsequent TCP::collect, you can see the full TCP response (HTTP status, version, headers, and HTTP payload) and change whatever you need. In this case, if the request URI matches what you're looking for, you'll remap the "200 OK" to "404 Not Found" in the TCP response.