views:

163

answers:

4

Our asp application is moving to new server and i want to implement a permenant url redirection. I am aware of following two approaches , i need to understand which one to user over other and when ?

Option 1:

<%@ Language=VBScript %><% Response.Redirect “http://www.new-url.com” %>

Option 2:

<%@ Language=VBScript %><% Response.Status="301 Moved Permanently" 
  Response.AddHeader "Location","http://www.new-url.com/" %>

Thanks,

Nikhil.

+1  A: 

Response.Redirect() (and the equivalent method RedirectPermanent() for a 301) does a lot of things behind the scenes. It null checks the requested URL string, encodes it, calls the event handlers for the Redirecting event if there are any, and finally calls Response.End(), which flushes the response back to the browser and aborts the current thread.

Ultimately, you probably won't notice much difference between setting headers manually and calling redirect.

Incidentally, there are more (and better) options for handling this. IIS has a URL Rewriting module, which would let you redirect a given URL without ever calling your page as a request handler, and centrally manage your URL's for easier management of search engines.

womp
This is not correct. See my answer for details.
Charles Boyung
What is not correct here?
womp
Exactly what I said in my answer. There is no way to have Response.Redirect do a 301.
Charles Boyung
You call Response.RedirectPermanent(). It just calls Response.Redirect(). Either way, how is my answer wrong?
womp
It does not call Response.Redirect internally. What in the world are you talking about? They both set headers and response status - neither one calls the other one.
Charles Boyung
As to how it is wrong - "Ultimately, you probably won't notice much difference between the two." That is a completely false statement. Response.Redirect does a 302 redirect. Setting the response status and header directly like he shows does a 301 redirect. If you don't know the difference between the two, you shouldn't be answering any web questions at all.
Charles Boyung
Just for reference, here's the code for RedirectPermanant:public void RedirectPermanent(string url){ this.Redirect(url, true, true);}
womp
A: 

A normal redirect will by default use HTTP status 302. A redirect with status 301 will not be indexed by searchbots (like Googlebot) and if they were, they will be removed from existing indexes. Very useful if you want to "update" an old URL to a newer URL. The searchbot will index redirects with status 302 anyway, so you may likely end up with pollution in search results. You'd normally use status 302 for for example PRG pattern and status 301 for permanent redirects like as you're doing now.

BalusC
+3  A: 

Response.Redirect issues a 302, which is a temporary redirect. 301, using the Response.AddHeader that you listed, is for permanent redirects.

The differences between 301 and 302 have some importance with search-engine-optimization. A 301 will hold all of your search rankings from the old location. On the flip side, if you DON'T want your new page to be indexed, you can use a Response.Redirect (302) since the engines will consider the redirect temporary. Google doesn't index 302's because a lot of spammers use it to try to increase their rankings.

Since you're permanently moving to a new server, a 301 is the best way to go.

Nate Dudek
A: 

Response.Redirect sends a "302 - moved temporarily" status code to the browser, which may or may not be okay depending on what you are doing. If you are redirecting to the correct location for your content, you want to do the 301 redirect because search engines will not crawl properly against a 302.

Charles Boyung
This is not 100% correct. Response.Redirect only sends a 302 if you set the "permanent" flag to false. It can send either a 301 or 302. He has the option of doing both, he doesn't need to manually set the headers to send a 301.
womp
@womp - you have absolutely no idea what you are talking about. There is no "permanent" flag. The Response.Redirect function takes one or two parameters - target URL and optionally a boolean to end the response. Please educate yourself: http://msdn.microsoft.com/en-us/library/a8wa7sdt.aspx
Charles Boyung
@womp - In case you can't read that whole page - here's the important part for you - "ASP.NET performs the redirection by returning a 302 HTTP status code."
Charles Boyung
You're taking the question way more literally than I am. He can use Response.RedirectPermanent() if he wants to send a 301. I interpreted his question as "set headers manually or use redirect?". You still haven't said what was wrong with my answer.
womp
@womp - He asked specifically about Response.Redirect vs. setting response headers. Your answer is completely wrong in that regard. You also made no reference to that function in your answer at all. And Response.RedirectPermanent is brand new to Asp.Net 4.0, so its actually pretty likely that he isn't using that, otherwise he would have referred to that.
Charles Boyung
@womp - Since he asked as specific question about two methods, don't you think you SHOULD be taking it literally? He didn't ask about some magic function where you set some mythical flag that doesn't exist in .Net, like your answer and comments suggest he do.
Charles Boyung
He asked a specific question about "two approaches" to URL redirection, not specific functions. The gist of *my* answer was what Redirect() did behind the scenes instead of setting the headers manually. *I didn't even mention 301 vs 302* and *neither did the OP*. My interpretation was entirely valid. Your hostility is baffling.
womp
@womp - Sure looks like he asked for the difference between two specific things - Response.Redirect and a 301 redirect. Otherwise, he wouldn't have mentioned setting 301 status specifically. When you state that they are the same thing, you are completely wrong.And while we're at it, since you voted my answer down, what is wrong with my answer; you know, the one that actually answers his question?
Charles Boyung
Nothing is wrong with your interpretation or answer - I took offense to your snarky attitude and hostile comments, and downvoted you. You'll find that happens.
womp
Nothing hostile about my comments - when you are wrong, you are wrong. If you can't handle that, then maybe you should go hide in a box somewhere.
Charles Boyung