Append trailing slash on all URLs

Hey all,

Was wondering if there’s a way to force a trailing slash on all route helpers.
I tried to specify a trailing slash in the router get("/foo/bar/", FooController, :bar),
but foo_path(@conn, :bar) outputs without the trailing slash /foo/bar.

I would also like to have it trailing in resources, so for a :show path, it would output /foo/:id/.

I personally hate the trailing slash, but I’m migrating from a system which forces it.
I also already have nginx rewriting anything w/o a trailing slash, but wanted to get the links pointing correctly to avoid unnecessary rewrites.

Thanks for any help!

1 Like

did you found an answer ? I’m searching for it as well(for the same reasons :frowning: )

1 Like

No, I ended up not using the route helpers.

@josevalim said some days ago that “Phoenix ignores the trailing slash when matching…”:

If you match with trailing slash in the router file, it would be useful for the helper functions to return with trailing slashes as well. I have no idea if there’s some technical detail that doesn’t easily allow for this.

trailing slash vs non-trailing slash is like the www vs non-www discussion :smile:

2 Likes

Hmm, that would be a useful way to make it explicit on which to use.

1 Like

Well for what is worth i did it other way around with simple Plug :wink: which is redirect 301 the url with “/” to no-slash(I prefer this way). It was for SEO optimisation our “expert” said that url with trailing slash “are better for SEO” :ghost:but after some googling google blog itself says that it doesn’t really matter far as you do 301 from one style to another it doesn’t count as double content.
I post the link here in case someone else like me will have to do it for “SEO reasons”

5 Likes

I think it doesn’t matter for SEO if you choose one or the other and never change it, but it might simplify things to build sites with the trailing slash, because that’s the way servers naturally work with HTML.

Many sites are now built with static site generators and have trailing slashes because each page is an index.html file in a directory. It’s possible to remove the slashes, but I think that just adds another thing that can go wrong (e.g., forgetting to implement those server rules when switching hosting — maybe one host like Netlify requires checking a box in a dashboard, another one like Firebase requires some rule buried in a 10,000+ line JSON file, and another one requires custom NGINX rules).

In the end it doesn’t really matter, but that’s why I choose the trailing slash if there is a choice. (I often use static site generators and Django, which both use trailing slashes by default.)