Optimize Website Design (11): Avoid redirection

Source: Internet
Author: User
Preface

The optimization of website design is a big topic. There are some general principles and some suggestions for different development platforms. This research has never been stopped, and I have shared this topic on different occasions.

As a general principle, Yahoo's team of engineers once provided 35 best practices. For this list, seeBest practices for speeding up your web siteHttp://developer.yahoo.com/performance/rules.html, at the same time, they also integrated a corresponding test tool yslow http://developer.yahoo.com/yslow/

I strongly recommend that all website developers learn these best practices and apply them based on their actual projects. In the next period of time, I will combine the ASP. NET development platform and use a seriesArticleTo help you better understand and use these principles.

Preparations

Prepare the following development environment and tools to follow me for subsequent learning.

    1. Google Chrome or Firefox, and install the yslow extension component. Please note that this component is provided by Yahoo, but there is no version for IE currently.
      1. Https://chrome.google.com/webstore/detail/yslow/ninejjcohidippngpapiilnmkgllmakh
      2. Https://addons.mozilla.org/en-US/firefox/addon/yslow/
      3. You should have some knowledge about the developer tools of these browsers. You can call up this tool by pressing F12.
    2. VISAUL studio 2010 SP1 or later, Visual Studio 2012 is recommended
      1. Http://www.microsoft.com/visualstudio/eng/downloads
    3. You need to have a good understanding of the basic development process and core technologies of ASP. NET. This series of articles is difficult to popularize basic knowledge.
Topics to be discussed in this article

I will discuss 11th principles in this article: Avoid redirects (avoid redirection ).

Redirection means that the user's original request (such as request a) is redirected to another request (such as request B ). This is a technology and phenomenon that already exists in the HTTP world. It does not have the so-called good or bad, and it does exist for some reason. Therefore, in the HTTP protocol, two status codes are defined to identify this scenario. They are:

    1. 301 moved permanently: the Status Code indicates that the requested resource is moved to another location. After the client receives the response, it needs to initiate another request to download the required resource. This status code may be associated with another status code 308 (308 permanent redirect (approved as experimental RFC) [12]) in the future (this has not yet been finalized as a standard)
    2. 302 found: This Status Code indicates that the requested resource has been found, but it is not in the original location. The server will reply to another address. After the client receives this response, you also need to initiate another request to download the required resources. This status code may be associated with the other two status codes in the future.
      • 303 see other (since HTTP/1.1)
      • 307 temporary redirect (since HTTP/1.1)

Currently, we only need to distinguish between 301 and 302. What are their essential differences? In fact, it is not difficult: 301 indicates permanent redirection, and 302 indicates temporary redirection. For general users, you may not be able to understand their differences, because they all need to be redirected. But it is of extraordinary significance for search engines. As we all know, search engines need to crawl website resources from time to improve the corresponding index structure. When a resource is permanently redirected (301), the search engine will intelligently know that the record in the index should be the new address after the permanent redirection, rather than the old address, in this way, you can avoid the need to first go to the old address and then redirect to the new address when querying through the search engine. For temporary redirection (302), this will not be done.

 

What is the impact of redirection?

With so many theoretical knowledge, let's take an example to see how specific redirection occurs.

The first request is the default. aspx page. For some reason, this request needs to be redirected to another page, product. aspx. Therefore, the server first returns the status code 302 for the default. aspx request, indicating that temporary redirection is required. Then, the response header contains the new address: product. aspx. As shown in ). In the response body, it is blank.

After the browser receives this reply, it re-initiates a request, product. aspx, which is processed normally and returns the status code 200.

This is the case. This obviously increases the number of browser-to-server round trips, which violates another principle: make fewer HTTP requests.

 

How redirection occurs

In fact, redirection often occurs. There are two main scenarios where redirection occurs

    1. Some behaviors of the server itself (for certain requests)
    2. ProgramSpecifically made redirection

The first case is very interesting. Different servers may behave differently during processing. For example, let's look at the following request.

Http://cnblogs.com

Through monitoring we found that first there will be a 301 redirection, then the real request (return 200), at this time the address is the http://www.cnblogs.com

Why? If you have website development experience, you must know that domain names all need to be resolved. In fact, the service provided to users is a network host on the Internet. If you want to access a website in a better way, you can access cnblogs.com or www.cnblogs.com in the preceding example. The difference is that the former has one redirect request.

 

If you take it for granted, you are wrong. Let's take a look at the famous 12306.cn.

In this case, the access fails. You must enter www.12306.cn. This is a simple and low-level question.

In fact, it is very easy to achieve, at the expense of a little bit of performance, for the user, improve the user experience. Here is a question:Someone can explain how to achieve this (cnblogs.com =>Www.cnblogs.com?

This is a typical redirection, which does not require anyCode. It is actually a server function. Of course, we should try our best to use www.cnblogs.com (for example, the link in the mail to people, try to use this path ). However, another method is provided, which is a good design.

 

There is also a typical redirection. Let's take a look at the example below. Please enter Google.com in your browser

The page we finally see is http://www.google.com.hk/

You will find that there will be several redirection

The first redirection is 301, redirecting from Google.com to www.google.com

The second redirection is 302, redirecting from www.google.com to http://www.google.com.hk/url? Sa = P & HL = ZH-CN & Pref = hkredirect & pval = Yes & Q = http://www.google.com.hk/&ust=1367722843791916&usg=AFQjCNEzkTX2uE5Jlo3NkA1vSHdwoCnnZQ

The third redirection is 302, from http://www.google.com.hk/url? Sa = P & HL = ZH-CN & Pref = hkredirect & pval = Yes & Q = http://www.google.com.hk/&ust=1367722843791916&usg=AFQjCNEzkTX2uE5Jlo3NkA1vSHdwoCnnZQ redirect to http://www.google.com.hk

 

In the same way, we can see from bing.com. We enter bing.com, but the actual page is cn.bing.com.

Actually, a redirection occurs here.

 

Why does Google and Microsoft Design the search engine's main interface like this? The reason is that they want to provide users with more personalized local services. Therefore, users in different countries and regions actually have independent hosts for processing.

Microsoft's approach is a bit different. They use the unified domain name bing.com, but they have prepared different hosts for different countries and regions, such as cn.bing.com and hk.bing.com.

 

There is also a redirection that is easier than we ignore. See the following example.

The sample website contains a folder named products, which contains many pages, for example, at least one default. aspx. We all know that the default. aspx is a default page. to access this page, you do not need to enter default. you can access the folder name directly, as shown in the following figure.

Http: // localhost: 9071/products

As you think, such a short path will return default in the products directory. the content of the ASPX page (this is good, right). However, through monitoring, we found that such a request would have a redirection.

Interestingly, it will redirect the address to http: // localhost: 9071/products/(only a path slash is added than the original address). Is it amazing? But this is true. If the access address does not contain a file name suffix (such as aspx or ASP), the server will try to resolve it to a folder and automatically add a path slash, then find the internal default page.

[Note] This behavior does not exist in ASP. net mvc, because ASP. net mvc request processing is routed.

 

Redirection in ASP. NET

If the above website is developed using ASP. NET (cnblogs.com is obviously true, while Google.com is obviously not. Will bing.com be true ?), So how can we achieve the aforementioned redirection?

ASP. NET 4.0 has the following methods for redirection:

    1. Response. Redirect http://msdn.microsoft.com/EN-US/library/08za4s98 (V = vs.110, D = hv.2). aspx
      • This will be a temporary redirection, that is, 302 is returned.
    2. Response. redirectpermanent http://msdn.microsoft.com/EN-US/library/dd322058 (V = vs.110, D = hv.2). aspx (this is the newly added method)
      • This will be permanently redirected, that is, 301 is returned.
    3. Server. Transfer http://msdn.microsoft.com/EN-US/library/y0w8173d (V = vs.110, D = hv.2). aspx
      • This will not generate 301 or 302. In fact, it directly returns 200, and the browser will not know that a redirection has occurred and there will not be an extra request.
        • It sounds good, isn't it? So,Is there any additional problem with this??

 

ASP. NET redirection and appending

 

I believe that you are familiar with at least one or two of the above three methods, but many people do not know their prices.

In fact, both the redirect and transfer methods call response internally. end method (this is easy to understand, because the redirection is required, so the current request should no longer provide a response ), however, this method will cause the processing thread related to the request to be forcibly interrupted. Specifically, this will cause an exception (Threadabortexception), Through try... Catch.

As we all know, exception handling is done by the CLR in. net, and the cost of exception handling is high. If an exception is thrown too frequently, it will have a significant impact on the performance.

For this question and how to improve it, refer to the official Microsoft documentation.

Http://support.microsoft.com/kb/312629/en-us

The solution section I extracted is as follows:

To work around und this problem, use one of the following methods:

    • ForResponse. End, CallHttpcontext. Current. applicationinstance. completerequestMethod insteadResponse. EndTo bypass the code execution toApplication_endrequestEvent.
    • ForResponse. Redirect, Use an overload,Response. Redirect (string URL, bool endresponse)That passesFalseForEndresponseParameter to suppress the internal callResponse. End. For example:

      Response. Redirect ("nextpage. aspx", false );

      If you use this workaround, the code that followsResponse. RedirectIs executed.

    • ForServer. Transfer, UseServer. ExecuteMethod instead.

 

How to Avoid redirection as much as possible

From the above examples and analysis, redirection cannot be completely avoided, and proper use of redirection can provide better functions for the website. (Such as localization and user experience ).

However, too many redirection operations will certainly have a significant impact on the website performance. So what methods can be used as a reference for us to improve this point?

    1. when defining the href attribute of a link address, try to use the most complete and direct address. For example,

      • Use www.cnblogs.com instead of cnblogs.com
      • Use cn.bing.com instead of bing.com
      • Use www.google.com.hk instead of Google.com
      • Use www.mysite.com/products/ instead of www.mysite.com/products
    2. when using response. Redirect, set the second parameter to false.
      • consider whether server. Execute can be used in Place
      • consider respone. redirectpermanent
    3. If migration from the test environment to the production environment is involved, we recommend that you use the cname mechanism in DNS to define aliases, rather than force redirection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.