92

Whats difference between Redirect() and RedirectPermanent(). I had read some articles, but I don't understand when we must use Redirect() and RedirectPermanent(). Can you show a pieces of example.

tereško
  • 58,060
  • 25
  • 98
  • 150
IFrizy
  • 1,705
  • 4
  • 17
  • 33

3 Answers3

162

The basic difference between the two is that RedirectPermanent sends the browser an HTTP 301 (Moved Permanently) status code whereas Redirect will send an HTTP 302 status code.

Use RedirectPermanent if the resource has been moved permanently and will no longer be accessible in its previous location. Most browsers will cache this response and perform the redirect automatically without requesting the original resource again.

Use Redirect if the resource may be available in the same location (URL) in the future.

Example

Let's say that you have users in your system. You also have an option to delete existing users. Your website has a resource /user/{userid} that displays the details of a given user. If the user has been deleted, you must redirect to the /user/does-not-exist page. In this case:

If the user will never be restored again, you should use RedirectPermanent so the browser can go directly to /user/does-not-exist in subsequent requests even if the URL points to /user/{userid}.

If the user may be restored in the future, you should use a regular Redirect.

Meryovi
  • 6,121
  • 5
  • 42
  • 65
  • 5
    http://stackoverflow.com/questions/1393280/http-redirect-301-vs-302-permanent-vs-temporary – monkeyhouse Jul 07 '13 at 23:24
  • 9
    Note that `RedirectPermanent` can be annoying while debugging because the second time you click, controller's action will be skipped and no break point are reached in the original function if it redirect elsewhere. That is unless you empty your cache before every click. – Antoine Pelletier Jun 27 '18 at 18:04
  • If you ever made the mistake of misusing the permanent redirect, is there a way to communicate that to a browser that has already visited and received a permanent redirect? – eaglei22 Dec 06 '18 at 19:15
  • @eaglei22 It's been two and a half years so you probably have that figured out. But just in case (and for future readers), dropping web history will do the trick. And yes, it suck a bag of crazy monkeys. That's why I use a different browser for development than for my actual browsing. Yes, that sucks too, on occasion. :) – Konrad Viltersten Jul 20 '21 at 05:01
  • @Meryovi: I got a scenario where I wanted to redirect the user to a different URL lets says example.com (an external url), then which option is more recommended? tx! – AaBa Nov 25 '21 at 03:48
18

RedirectPermanent is 301 and Redirect is 302 status code

dm03514
  • 54,664
  • 18
  • 108
  • 145
  • 14
    For reference, Browsers may remember the 301 and never hit the original URL again if they desire, while a 302 indicates that they should re-check every single time if the redirect is still there. Actual implementations may of course vary, but that's the intent behind 301 and 302 – Michael Stum Jul 07 '13 at 23:23
1

They send different response codes to the browser. 301 is a permanent redirect, 302 a temp one. The end effect is the same, but if the client wants to index links (the most common client that does this will be search engines) then a permanent redirect tells the client to update its records to ignore the old link and start using the new one. A temp redirect tells the client that the page is redirecting for now, but not to delete the old link from its indexing database