Forum Moderators: phranque

Message Too Old, No Replies

Redirect based on http response

How to use mod_rewrite to redirect based on the response code

         

albert newton

3:53 pm on Jul 13, 2009 (gmt 0)

10+ Year Member



Hi, I am trying to use mod_rewrite to be able to redirect to my custom html error page, when a 404 is returned. Right now, I have my http server running and my appserver(Websphere) running. When I take down a service on the appserver, it returns a message as follows:

SRVE0255E: A WebGroup/Virtual Host to handle localhost:80 has not been defined.

I want to be able to redirect to my custom error message, based on the response, rather than show this error message. Trying to use the ErrorDocument directive within the httpd.conf file does not work,... but I have heard that this is possible to do with mod_rewrite. I am not sure how to do this.

jdMorgan

5:11 pm on Jul 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you "redirect" to an error page, then what will be returned is a redirect (301 or 302) status, and not a 404 or 410. This will make a large mess of things.

I'd suggest looking into the mechanism that you use to 'map' URLs to your 'services' and making that mechanism 'aware' or the services that have been removed. If a service has been removed, then (dependping on your server version and the 'mapping' mechanism you use) either rewrite removed-service URLs to a non-exsistent filepath or directly return a 410 or 404 response.

Alternately, do not 'remove' any services, but instead replace them with a script that will directly return a proper 410-Gone status response.

Overall the proposed method is 'closing the barn door after the horses have fled' in that mod_rewrite works in the URL-to-filename translation phase of the API as a request is received from the client; By the time the server knows the response (after the content-handling phase), it is far too late to run mod_rewrite.

Jim

albert newton

6:15 pm on Jul 13, 2009 (gmt 0)

10+ Year Member



I think you misunderstood my question... I already have a 404 being returned to me right now. This happens since I try to hit a service that is down(the service has been turned off) from the app server(Websphere). I need to be able to write a mod_rewrite module that sees this 404 response and then redirect it to an error page. Basically I am having a hard time trying to figure out the code which can see this reponse( the 404 response). Once I do this, the next step would be to redirect to an error page since the response is a 404.

jdMorgan

6:32 pm on Jul 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I suspect that you've got a 404 response being returned to your client (browser), not to your front end server. Is that correct?

Also, do not overlook what I said above. It may just be loose terminology use, but do not "redirect" to an error page. Either serve one up directly, or 'include' it for output by a script.

It sounds to me like you'll need to put a 'wrapper' around your back-end requests: Take an incoming client request for a 'service' and rewrite it to a front-end script. Have that script forward the request to the back-end app server, and then accept the back-end's response. Then examine that response and either return the back-end's output or an error response (as appropriate) to the client.

Jim

Caterham

7:45 pm on Jul 13, 2009 (gmt 0)

10+ Year Member



How do you connect? mod_proxy et al.? See ProxyErrorOverride to get your ErrorDocument working.

albert newton

8:16 pm on Jul 13, 2009 (gmt 0)

10+ Year Member



Yes, what seems to be happening is that the 404 response is being returned to the app server( Websphere) and not the front end server( IBM Http server) that lies in front of it. The plugin that connects the two of them probably just sees that it gets the response. Since it gets a response, it is not going to look at the response but will just forward it to the front end server( the http server). The http server sees that it received a response, but does not try to see that the response that it received is a 404 response, and hence does not show the error page. Thus, I have been trying to find some way using the mod_rewrite module which can force it to scan the response. After that I can decide how I want to show the error page ( that is secondary at the moment).

Also, I am not using any proxy or so to connect, so I don't think I would probably need to use ProxyErrorOverride.

Caterham

1:40 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



front end server( IBM Http server)

That is a fork of apache httpd but with modifications and is closed-source. Unless they use their own connector protocol they'll likely proxy via http to connect the backend server. A way to modify responses at apache httpd 2+ are output filters with a tiny module.

I have been trying to find some way using the mod_rewrite module which can force it to scan the response.

mod_rewrite does not register output filters. mod_rewrite registers (besides config hooks) a translate name (per-server context rewrite), two fixups (per-dir context rewrite, associating handler and mime-type) and a content handler hook (internal redirect).

albert newton

3:22 pm on Jul 15, 2009 (gmt 0)

10+ Year Member



Can you give me an idea of how I could use an output filter with a module to modify responses?

Caterham

5:47 pm on Jul 15, 2009 (gmt 0)

10+ Year Member



Take a look at the source code of mod_deflate how to register an output filter (ap_register_output_filter), check r->status etc.