Phoenix Route redirecting from /users to /users/

routes
routerhelpers

#1

Hi Everyone,
I need help.
i have a route
“/users”
how can i redirect it to “/users/”?
because in router it is consider the same. it redirects over and over again.
thanks


#2

Can you elaborate on what it is you’re trying to accomplish by doing this? Is it just aesthetic?


#3

i just want to redirect the “/users” to “/users/” but it is consider the same


#4

I get that. I want to understand why you want to do that. Why do you want to have the trailing prefix?


#5

bacause i have an external app ("/users/") and on my route in elixir “/users” i want it to move_permanently to “/users/” when they visit “/users”


#6

It is a semantic distinction made by some API designers, that a trailing slash indicates a collection ressources, while a missing slash indicates a single ressource.


#7

can you post the code that is not working?


#8

This is a guess, but I suppose having user_path(conn, :index) return /users/ instead of /users


#9

Phoenix ignores the trailing slash when matching but it should not remove them when generating a route. So if you do something like redirect conn, to: "/users/", then it should work.


Append trailing slash on all URLs
#10

Redirects can be cached in browsers, so make sure you test after clearing cache and/or in a private/incognito browser window.


#11

Caching can really be a hard thing… At least for google chrome I had to learn recently, that disabling the cache in the developer toolbar doesn’t actually disable the cache, but only parts of it…