cancel
Showing results for 
Search instead for 
Did you mean: 

HTTP response codes: redirects and 404's traffic script

SOLVED
Highlighted
New Contributor

HTTP response codes: redirects and 404's traffic script

I have the following script to redirect a server 404 responses to a nice 404_error.htm page:

 

if( http.getResponseCode() != 404 ) break;
http.setResponseCode("404 Not Found");
http.redirect( "/404_error.htm" ); 

 

The problem is that the script returns a 302 and then a 200 in http watch. I would like to see at 404 repose code returned before the redirect takes place.

 

Line two of the rule does not have the desired effect, it seems that I could either do a 404 or a 302. Is there a way of combining the two?

 

3 REPLIES
New Contributor

HTTP response codes: redirects and 404's traffic script

With any knowledge of the system itself:

If you redirect in the HTTP header, the response code is 302. You will get a 200 later because the file you get redirected to exists.

If you want to keep the 404 on the first response you will need to use some JavaScript in the HTTP body to do the redirection.

Edwin


Edwin

New Contributor

Re: HTTP response codes: redirects and 404's traffic script

Thanks for the response Edwin. I wll give the Java option a go.

Frequent Contributor

Re: HTTP response codes: redirects and 404's traffic script

Another option is to use the TrafficScript http.sendResponse() function to control how the 404 content is sent to the client:

bq. http.sendResponse( "404 Not Found", "text/html", $body,");

You'll need to fill $body with the content of your 404 message; either put it inline in your trafficscript rule, or pull it out of the 'extra files' store using resource.get().