[Rails] How to do a 301 redirect
I had to dig a little for this. The answer is simply:
1 2 |
headers["Status"] = "301 Moved Permanently" redirect_to "/" |
Here is the relevant snippet from response.rb
in ActionPack:
1 2 3 4 |
def redirect(to_url, permanently = false) @headers["Status"] = "302 Found" unless @headers["Status"] == "301 Moved Permanently" @headers["location"] = to_url ... |
So as you can see, it actively checks for that particular header. Kinda weird that it doesn’t have a :status
flag like so many other methods, but that’s the way it is.

About Calvin Correli
I've spent the last 17 years learning, growing, healing, and discovering who I truly am, so that I'm now living every day aligned with my life's purpose.
Read next
Deleting photos in iPhoto
Learn, grow, heal, and discover your life's purpose
Join my free newsletter
No spam. Just timeless insights and ideas.
3 comments
Leave a comment