Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Catalyst - Add Trailing Slashes to URLs

by Sixtease (Friar)
on Dec 02, 2008 at 13:28 UTC ( [id://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 (Monsignor) 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...


    www.jasonkohles.com
    We're not surrounded, we're in a target-rich environment!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://727410]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-19 14:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found