All posts

301 vs 302 redirects using Phoenix

If you need a temporary redirect aka 302 using the redirect/2 function:

def redirect_show(conn, %{"id" => id}) do
  conn
  |> redirect(to: Routes.blog_path(conn, :show, id))
end

If you are permanently moving a page and want to indicate that to mother Google and the rest of the web you need to first execute the put_status/1 function and then pipe to redirect/2.

def redirect_show(conn, %{"id" => id}) do
  conn
  |> put_status(:moved_permanently)
  |> redirect(to: Routes.blog_path(conn, :show, id))
end

Check out other Phoenix posts:

Postscript:

I built two products to help fund continued efforts for this website, it would mean a lot if you would check them out:
1. 2500+ Words for the Undermotivated Software Engineer - A short essay I wrote on the titled topic ($3).
2. Build 3 Chrome Extensions in Under 2 Hours - A course I built to teach chrome extension development ($9-20).