Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Catalyst - Add Trailing Slashes to URLs

by Sixtease (Friar)
on Dec 02, 2008 at 13:28 UTC ( #727410=perlquestion: print w/replies, xml ) Need Help??
Sixtease has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I'm looking for a way to tell Catalyst to add a trailing slash if one is not present to the URL of the request and redirect there. But only if the request is not being handled by Static::Simple.

I could do it by checking the URL in every action but I'd prefer to have it globally in one place. How can I do that?

use strict; use warnings; print "Just Another Perl Hacker\n";
  • Comment on Catalyst - Add Trailing Slashes to URLs

Replies are listed 'Best First'.
Re: Catalyst - Add Trailing Slashes to URLs
by dsheroh (Prior) on Dec 02, 2008 at 15:38 UTC
    Personally, I'd use mod_rewrite to append the / to any URIs that don't end in an extension from a defined set - probably \.(html?|css|jpg|gif|png|ico)$ - rather than having Catalyst do it, on the assumption that anything handled by Static::Simple will be ending with one of those extensions.

      But if I'm not mistaken, then this would not add the slash in the user agent. And that's where it is essential because of relative links

      use strict; use warnings; print "Just Another Perl Hacker\n";
        By default you are correct, but adding [R] to the end of your RewriteRule will cause it to do a client-side redirect instead of handling it internally.
Re: Catalyst - Add Trailing Slashes to URLs
by jasonk (Parson) on Dec 04, 2008 at 04:45 UTC
    package MyApp::Controller::Root; use strict; use warnings; use parent 'Catalyst::Controller'; __PACKAGE__->config->{ 'namespace' } = ''; sub auto : Private { my ( $self, $c ) = @_; if ( $c->request->path !~ m{/$} ) { $c->response->redirect( $c->request->path.'/' ); return 0; } return 1; }

    I could have sworn there was a plugin out there to do this, but I can't seem to find it now...
    We're not surrounded, we're in a target-rich environment!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://727410]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2018-05-20 12:19 GMT
Find Nodes?
    Voting Booth?