You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Ian Grant <ia...@cl.cam.ac.uk> on 2007/01/03 17:03:22 UTC

302 redirects from scripts.

Dear mod_perl types,

A system I have installed relies on doing redirects like the code below, but 
the redirects don't happen, I just get blank pages in the browser.

I have tested temp redirects in httpd.conf and they work fine.

Can anyone tell me if the code below should work, or is there something I need 
to call to get the Location header output?

use strict;

use Apache2::RequestUtil ();

my $request=Apache2::RequestUtil->request();
my $url='https://www.some.place.ac.uk/perl/newscript';

$request->status_line("302 Moved");
$request->headers_out->{'Location'} = $url;
print STDERR "redir: redirecting to $url\n";

exit;

Re: 302 redirects from scripts.

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
Perrin Harkins wrote:
> Geoffrey Young wrote:
> 
>>actually, REDIRECT is an alias for HTTP_MOVED_TEMPORARILY.  from
>>modperl_constants.c:
>>
>>  if (strEQ(name, "REDIRECT")) {
>>    return newSViv(HTTP_MOVED_TEMPORARILY);
>>  }
> 
> 
> Isn't that basically a coincidence though?  I thought the non-HTTP
> constants were subject to change at any time, which is why they're defined
> in the core instead of hard-coding numbers in scripts.

in this specific case it's historical.  httpd-1.3.X did this in httpd.h:

  #define HTTP_MOVED_TEMPORARILY             302
  ...
  #define REDIRECT            HTTP_MOVED_TEMPORARILY
  #define USE_LOCAL_COPY      HTTP_NOT_MODIFIED
  #define BAD_REQUEST         HTTP_BAD_REQUEST
  #define AUTH_REQUIRED       HTTP_UNAUTHORIZED
  ...

and mp1 passed all these along.

httpd-2.0 did away with REDIRECT as a constant, kinda forcing everyone
to use the HTTP_* form of things.  early on in mp2 construction enough
people complained that we added mod_perl-specific aliases for the most
common of the old httpd-1.0-style constants (REDIRECT, SERVER_ERROR,
etc) to make the transition easier.

so, in short, REDIRECT is valid and officially equivalent to 302 in
apache 1.0, but in apache 2.0 it's not so we've faked it in mod_perl
land for the sake of our users (right or wrong).

--Geoff

Re: 302 redirects from scripts.

Posted by Perrin Harkins <pe...@elem.com>.
Geoffrey Young wrote:
> actually, REDIRECT is an alias for HTTP_MOVED_TEMPORARILY.  from
> modperl_constants.c:
>
>   if (strEQ(name, "REDIRECT")) {
>     return newSViv(HTTP_MOVED_TEMPORARILY);
>   }

Isn't that basically a coincidence though?  I thought the non-HTTP
constants were subject to change at any time, which is why they're defined
in the core instead of hard-coding numbers in scripts.

- Perrin


Re: 302 redirects from scripts.

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
Perrin Harkins wrote:
> Jonathan Vanasco wrote:
> 
>>	$self->{'ApacheRequest'}->status(Apache2::Const::REDIRECT);
> 
> 
> Don't do that.  That's an internal apache code, not an HTTP code.  The
> HTTP code would be something like Apache2::Const::HTTP_MOVED_TEMPORARILY.

actually, REDIRECT is an alias for HTTP_MOVED_TEMPORARILY.  from
modperl_constants.c:

  if (strEQ(name, "REDIRECT")) {
    return newSViv(HTTP_MOVED_TEMPORARILY);
  }

the thing that usually trips people up is HTTP_OK is 200 but OK is 0, so

  $r->status(HTTP_OK)

is proper but

  $r->status(OK)

makes no sense - as perrin correctly pointed out, $r->status() always
needs to be set to an HTTP status code (like 200 or 302) and not an
internal apache one (like OK, DECLINED, or DONE).

--Geoff


Re: 302 redirects from scripts.

Posted by Perrin Harkins <pe...@elem.com>.
Jonathan Vanasco wrote:
> 	$self->{'ApacheRequest'}->status(Apache2::Const::REDIRECT);

Don't do that.  That's an internal apache code, not an HTTP code.  The
HTTP code would be something like Apache2::Const::HTTP_MOVED_TEMPORARILY.

> 	return Apache2::Const::REDIRECT;

That's the only correct place to use the REDIRECT constant.

- Perrin


Re: 302 redirects from scripts.

Posted by Jonathan Vanasco <jv...@2xlp.com>.
On Jan 3, 2007, at 11:24 AM, Geoffrey Young wrote:

>> my $request=Apache2::RequestUtil->request();
>> my $url='https://www.some.place.ac.uk/perl/newscript';
>> $request->headers_out->{'Location'} = $url;
>> $request->status_line("302 Moved");


Personally, I do this:

	$self->{'ApacheRequest'}->headers_out->set( Location=> '/my/home' );
	$self->{'ApacheRequest'}->status(Apache2::Const::REDIRECT);
	return Apache2::Const::REDIRECT;

// Jonathan Vanasco

| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
- - - - - - - - - - - - - - - -
| FindMeOn.com - The cure for Multiple Web Personality Disorder
| Web Identity Management and 3D Social Networking
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
- - - - - - - - - - - - - - - -
| RoadSound.com - Tools For Bands, Stuff For Fans
| Collaborative Online Management And Syndication Tools
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
- - - - - - - - - - - - - - - -



Re: 302 redirects from scripts.

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
Ian Grant wrote:
> Dear mod_perl types,
> 
> A system I have installed relies on doing redirects like the code below, but 
> the redirects don't happen, I just get blank pages in the browser.
> 
> I have tested temp redirects in httpd.conf and they work fine.
> 
> Can anyone tell me if the code below should work, or is there something I need 
> to call to get the Location header output?
> 
> use strict;
> 
> use Apache2::RequestUtil ();
> 
> my $request=Apache2::RequestUtil->request();
> my $url='https://www.some.place.ac.uk/perl/newscript';
> 
> $request->status_line("302 Moved");

don't alter the status line directly.  instead use

  $request->status(302);

from Registry scripts.  once you move to proper mod_perl handlers,
simply calling

  return Apache2::Const::REDIRECT;

is sufficient - no call to $request->status() required.

> $request->headers_out->{'Location'} = $url;
> print STDERR "redir: redirecting to $url\n";
> 
> exit;

better form to

  return Apache2::Const::REDIRECT;

than to call exit - since mod_perl processes never really exit this call
ends up being overridden anyway.  just fyi :)

HTH

--Geoff