[mod_python] Internal redirect (to the same server)

Ian Clelland ian at veryfresh.com
Thu Aug 15 15:37:04 EST 2002

On Thu, Aug 15, 2002 at 12:19:30PM +0200, Grant Beasley wrote:
> Hi
> If I want to redirect the browser to another URL on my server, I can use a
> HTTP redirect, but as I understand it, the redirect gets sent to the
> browser, which then goes to the new URL, i.e. comes back to my server.

Yep. An HTTP redirect is a (usually) 301 status code sent back to the 
browser to tell it to request a different document. This allows for a 
great deal of flexibility on the server side -- you can go as far as 
redirecting clients to a completely different server, or to a different 
protocol (ftp: or mailto:, even).

> I
> want to be able to tell apache to serve another URL, without the useless
> loop back to the browser.
> Is this possible? And if so, could you give me a few pointers?

If the extra messages really are useless, then the way to eliminate 
them is to change the URL on the server before Apache handles it. The 
simplest method is probably to use mod_alias (or mod_rewrite if you 
need something more powerful).

If you want to do this completely in mod_python, then you can write a 
PythonTransHandler, which will receive all requests, and has the chance 
to modify the URL before Apache passes the request to your main handler.

Remember, though, that in these cases, the client software has no idea 
that the URL has been changed, and will continue to request the old URL 
every time. This can be very useful if you are trying to create a 
'virtual' server layout which is different than your actualy filesystem 
layout, but at other times, you just want to tell the client 'sorry, 
this document has moved; please look for it here now.' That's when the 
HTTP redirects are useful.

> Secondly, what is the difference between HTTP_MOVED_TEMPORARILY and

HTTP/1.0 only had two redirection status codes: 301 (Moved Permanently) 
and 302 (Moved Temporarily). 302 was used for any situation where the 
client shouldn't remember the response, and should continue to request 
the original URL every time.

In HTTP/1.1, the 302 status code has been split into three different 
codes: 302 (now called 'Found'), 303 (See Other) and 307 (Temporary 

The idea behind this is that a 302 tells the client "I looked for the 
URL which you requested, and this is where I found it." It is not 
necessarily the case that the URL has changed, just that the document 
you want is somewhere else.

A 307 tells the client "The URL you requested has been temporarily 
moved. Please get it from this location, for now." This should be 
returned when the document used to be in one locaion, and probably will 
be back there again, but it has been moved out of the way for a while.

For all practical purposes, these two responses are exactly the same. 
The only difference (besides the semantics) is that HTTP/1.0 clients 
don't understand the 307 code at all, so if you want to use it, you'll 
have to check the protocol version (in mod_python, test req.proto_num 
>= 1001) and fall back to 302 if the client is using HTTP/1.0.

Neither of these status codes should usually be returned in response to 
a POST request, by the way. If a client receives a 302 or 307 from a 
POST request, then it is supposed to confirm the redirect with the 
user, and (after confirmation) repeat the POST to the new URL. A 303 
code can be returned to tell the client "I've processed your POST 
request, and the results you want are at this URL. Please use a GET 
query to access it."

Ian Clelland
<ian at veryfresh.com>

More information about the Mod_python mailing list