Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for client side vs. server side redirects: When to use what?

Tags:

I understand that most of the languages support server side redirects (asp.net: Response.Redirect, PHP: header( 'newpage' ) ; ). You could also do a redirect with JavaScript (window.location.href="newLocationURL").

When would you choose one over the other ?

With respect to ASP.net/IIS7(app pool in Integrated mode,enable 32 bit apps=false), I noticed that even when the page has a 302 header, the whole page body is sent to the client side.

And I believe this is not the case with PHP, only headers are sent ? To quote Redirect on client side means following steps:Client-side -> Server-side -> Client-side -> Server-side -> Client-side.

Redirect on Server-Side means:Client-side -> Server-side -> Client-side (headers only)* -> Server-side -> Client-side.

Is there a W3C standard or server side redirect implementation differ from one web server technology to another ?

Edit: I am only concerned about Response.Redirect (in asp.net) and not server.transfer, at least for this discussion

like image 918
ram Avatar asked Feb 22 '10 14:02

ram


People also ask

When should I use server transfer and response redirect?

Use "Server. Transfer" when you want to navigate pages which reside on the same server, use "Response. Redirect" when you want to navigate between pages which resides on different server and domain.

What is a server-side redirect?

A server-side redirect is a forwarding method in which the server sends a 3xx HTTP status code when a URL is requested. The server determines what URL visitors and search engines should be sent to.

What is client-side redirect?

A client-side redirect is implemented by code inside the content that instructs the user agent to retrieve content from a different URI. It is important that the redirecting page or Web page only contains information related to the redirect.


2 Answers

The JavaScript example is actually not a redirect. There's no means of a 301/302 response. It is just a simple request which happens during a certain Javascript event long time after the page is arrived. If you do this during page load, then it would have more overhead than a real redirect and it would not work on JS-disabled browsers as well.

Redirects are to be initiated from the server side with a 301/302 response. All webapp languages/frameworks defaults to 302. You can usually make it 301 by adding one extra parameter or code line which instructs that. The benefit of 301 is by the way that the particular request won't be indexed (anymore) by the searchbots.

like image 132
BalusC Avatar answered Oct 24 '22 02:10

BalusC


In ASP.Net, there is an important distinction between two kinds of server-side redirects. They are Response.Redirect and Server.Transfer.

If you call Response.Redirect, that entails two round trips to the server. On the first call to the server, the server response directs the browser to request the next page. Requesting that next page constitutes the second round trip to the web server.

If you use Server.Transfer, there is only one round trip. Therefore, there is much less network traffic. However, there is a limitation to using Server.Transfer, which is that the target page has to be on the same web server. That is, you can't Server.Transfer from your web app to www.Google.com. But you can Response.Redirect to it.

There are other details involved with using either of these approaches which you would want to research before using them. However, I believe that it is significant in the context of this question to note that in any language, Response.Redirect may result in much heavier network traffic than is really necessary.

like image 29
DOK Avatar answered Oct 24 '22 02:10

DOK