start page | rating of books | rating of authors | reviews | copyrights

Perl CookbookPerl CookbookSearch this book

21.4. Redirecting the Browser

21.4.1. Problem

You want to send a redirection back to the browser.

21.4.2. Solution

Use $r->header_out to set the Location header, then return REDIRECT:

$r->header_out(Location => "http://www.example.com/somewhere");
return REDIRECT;

21.4.3. Discussion

If you set the Location header and return REDIRECT, the client knows the address of the new page. This is called an external redirection, because the browser (external to the web server) handles the mechanics of requesting the new page. The URL should be a complete URL (with http, etc.), never a partial one.

An internal redirection is one where Apache sends back another page from the same site. The browser never knows that the page has changed, which means relative URLs from the page could be broken. Request an internal redirection with:

$r->internal_redirect($new_partial_url);
return OK;

Apache treats internal redirections almost as though they were new requests: each phase of the request cycle is called again for the new request. Unlike the Location header, internal_redirect takes only a partial URL. You should have no logic after calling internal_redirect other than to return OK.

21.4.4. See Also

Writing Apache Modules with Perl and C; Recipe 19.7; Recipe 8.5 in mod_perl Developer's Cookbook; the Apache.pm manpage



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.