Question

I am trying to redirect in the file /subdir/subdir/some.cgi which calls the following method:

$self->redirect_to("/anothersubdir/some.html");

However, the above call will redirect to /subdir/subdir/some.cgi/anothersubdir/some.html instead. So how do I really make it redirect to /anothersubdir/some.html?

Was it helpful?

Solution

Apparently you must provide an absolute path to redirect_to.

Why ? Because redirect_to says ...same arguments as url_for, which generates that url unless it is absolute. The docs aren't too explicit, but the source is.

So you could construct a new Mojo::URL object and give it to redirect_to:

my $url = Mojo::URL->new->base($self->req->url->base->clone)->base->userinfo(undef);
$url = $url->path('/anothersubdir/some.html');
$self->redirect_to( $url );

Or copy redirect_to and modify it to not use url_for

sub redirect_raw {
  my $self = shift;

  # Don't override 3xx status
  my $res = $self->res;
#~   $res->headers->location($self->url_for(@_)->to_abs);
  $res->headers->location();
  return $self->rendered($res->is_status_class(300) ? () : 302);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top