Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect with a 301 or 302 in the routes.rb

How can you redirect using a 302 from within the routes.rb file?

like image 251
Tom Rossi Avatar asked Oct 02 '13 13:10

Tom Rossi


People also ask

What is a 301 and 302 redirect?

The user's search experience may be the same as both options land the user on the appropriate webpage. However, search engines handle these types of URL redirects differently - the 302 redirect means that the page has been moved temporarily and other, 301, means that a new page has taken over permanently.

What is routes RB?

Routes are defined in the file config/routes. rb, as shown (with some extra comments) in Listing 3.1. This file is created when you first create your Rails application. It comes with a few routes already written and in most cases you'll want to change and/or add to the routes defined in it.

Is 302 permanent redirect?

What is a 302 redirect? Whereas a 301 redirect is a permanent relocation of your URL, a 302 redirect is a temporary change that redirects both users and search engines to the desired new location for a limited amount of time, until the redirect is removed.


1 Answers

You can pass in the status to the redirect in the route statement. For example, to do a 302 redirect:

In the routes.rb

get '/old/path', to: redirect('/new/path', status: 302) 

I hope this helps someone else!

like image 186
Tom Rossi Avatar answered Oct 12 '22 07:10

Tom Rossi