Search code examples
perlurlcatalysttrailing-slash

catalyst consistent url format with trailing slash


I'm developing a Catalyst application and having trouble with the way Catalyst interprets urls.

Let's say that in our Catalyst application we have a controller Account. If this is the case, Catalyst will interpret

http://mydomain.com/account and http://mydomain.com/account/

as the same url for the index action.

However, for SEO and linking purposes (and just to be consistent overall) I would like to force Catalyst to only recognize one format and stick to it.

I've found one module that seems to be built for this: Catalyst::Plugin::SanitizeUrl, it's documentation says you should just put

use Catalyst 'SanitizeUrl';

in myapp.pm and this will handle everything for you.

However, whenever I use it I just get this error:

Bad request

On every page load. Does anyone know of a simple way to have Catalyst only use one format?


Solution

  • I ended up combining what edem said with some stuff that I did and this is my end result:

    sub begin :Private {
        my ($self, $c) = @_;
    
        #don't allow trailing slashes!
        my @path = split "/", $c->req->path, -1;
        if(@path and (pop @path eq '')) {
            my $p = join '/', @path;
            #set as moved permanently
            $c->res->redirect("/$p", 301);
            return;
        }
    }
    

    If there is a trailing slash at the end of the request, it will always redirect to the same url but without the slash.