thojanssens1

thojanssens1

Phoenix - redirects in router file

It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.:

redirect "/", UserController, :list
get "/users", UserController, :list

This is typically needed when you have a URL that you want the user to be able to type but that should be redirected to a more specific URL.

Ofc I can code an action and a redirect myself, but such a helper would avoid me to add that code that could be explicitly defined in the router; you can then quickly see that there is a redirect along with the other routes, without inspecting the controller’s code. I think such redirects without any logic belongs there.

What do you think about that idea?

Marked As Solved

mathieuprog

mathieuprog

The redirect library addresses this shortcoming by providing with a redirect/3 macro, allowing to redirect a request from one path to another in the router.exfile:

redirect "/", "/users", :permanent # 301 redirect
redirect "/", "/users", :temporary # 302 redirect

https://github.com/mathieuprog/redirect

Also Liked

jakeprem

jakeprem

We ran into this on the project I’m working on. We found this blog post to be a pretty good way to do this: How to Redirect from the Phoenix Router | Viget

We ended up with a plug that lets us do this:

get "/", Redirect, to: "/datasets"

You can see our implementation here: smartcitiesdata/apps/andi/lib/andi_web/plugs/redirect.ex at master · UrbanOS-Public/smartcitiesdata · GitHub
smartcitiesdata/apps/andi/lib/andi_web/router.ex at master · UrbanOS-Public/smartcitiesdata · GitHub

It looks like there is a plug_redirect package in Hex that does adds a redirect keyword. Their Readme suggests using it as its own Plug in your endpoint, but at a quick glance I don’t see any reason you couldn’t use it in a Phoenix router

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I think this is a good idea, if a little niche. although you’d want to implement it as redirect "/", "/users" because redirect "/", UserController, :list is ambiguous. Multiple paths can route to the same controller action.

LostKobrakai

LostKobrakai

Phoenix.Controller.redirect/2 is a plug, and the generated router for phoenix has it imported by default. Together with forward you should be able to use it already without the need for additional code.

Where Next?

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2976 91332 914
New
f0rest8
Hi everyone :waving_hand: Posting here to showcase and announce that Metamorphic is now officially live on a public-facing domain at htt...
New
andrielfn
Hey there :waving_hand: I want to introduce Fluxon UI, a modern UI component library for Phoenix LiveView that I have been working on fo...
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
AstonJ
Just a general thread to post chat/news/info relating to AI/ML stuff that may be relevant for Nx now or in the future. Got anything to sh...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement