How to redirect back to previous page in Elixir & Phoenix?

Article autor
September 9, 2025
How to redirect back to previous page in Elixir & Phoenix?
Elixir Newsletter
Join Elixir newsletter

Subscribe to receive Elixir news to your inbox every two weeks.

Oops! Something went wrong while submitting the form.
Elixir Newsletter
Expand your skills

Download free e-books, watch expert tech talks, and explore open-source projects. Everything you need to grow as a developer - completely free.

Table of contents

In this post, you'll learn how to easily redirect users to the previous path using the Navigation History plug.

It's super easy, first add the plug to dependencies:

def deps do
  [ 
    ...,
    {:navigation_history, "~> 0.3"}]
end

Then, add it to your pipeline of choice (in most cases it's gonna be :browser):

pipeline :browser do
  ...
  plug NavigationHistory.Tracker
end

Now, you can use use available NavigationHistory.last_path/1 function to access the previous path:

redirect(conn, to: NavigationHistory.last_path(conn))

That's it! You can also exclude specific paths from navigation history:

plug NavigationHistory.Tracker, excluded_paths: ["/login", ~r(/admin.*)]

Find more details about this lib here: https://github.com/danhper/plug-navigation-history.

Until next time!

Check this article: Why Elixir & Phoenix is a great choice for your web app in 2022

Work with a team that keeps learning and building better software every day.

Related posts

Dive deeper into this topic with these related posts

No items found.

You might also like

Discover more content from this category

How to deal with timeout issue when debugging Phoenix app

There is a common scenario: You'd like to debug your Phoenix app with break!/4 or IEx.pry/0. Everything works fine, until... Phoenix server throws a timeout error statement.

How to change column to nullable with modify in Ecto migration

Sooner or later you'll have to change the null constraint in one of your DB relations. How to do it easily in Ecto?

Skip file changes tracking in git

So, you’re changing this one file for local development purposes only. Maybe it’s config, maybe some source file, but one thing is certain - you don’t want those changes to be committed. And what’s worse, .gitignore doesn’t work.