asp.net to achieve 404 page and 301 Redirect Method _ Practical Skills

Source: Internet
Author: User

This article illustrates the method of asp.net to achieve 404 pages and 301 redirects. Share to everyone for your reference. The implementation methods are as follows:

In a way, 301 redirects have nothing to do with 404 pages. Why I want to get together, because it is very simple to achieve, where I will introduce a.

How to set the 404 page method in the ASP.net record.
Below first look at the previous Setup method, Web.config file:

Copy Code code as follows:
<configuration>
<system.web>
<customerrors mode= "RemoteOnly" defaultredirect= "404.aspx" >
</customErrors>
</system.web>
</configuration>

Of course, we've created 404 page 404.aspx before, and I'm sure a lot of people think that's OK, but we've overlooked a point where we just do a simple redirect and don't tell the browser that the access page doesn't exist. A paragraph below is excerpted from Sun Ming Blog:
The 404 error is the seo/seo.html "target=" _blank > Search engine identifies the status of the Web page through an HTTP status code. When the search engine gets a bad link, the site should return a 404 status code, telling the search engine to discard the index on the link. If you return a 200 or 302 status code, the search engine will index the link, which leads to a large number of different links pointing to the same page content. As a result, search engines have significantly reduced their trust in the site. Many sites have this problem: the 404 page Returns a 200 or 302 status code instead of a 404 status code.
So, next I added the following code under the 404.aspx file:
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
Response.Status = "404 Not Found";
}

Here, by using the HttpWatch tool, you can find that a 404 status code is correctly returned when you visit a page that does not exist in the Web site.
So how to set 301 pages, let's look at the following

For example: This site is the original domain name is www.jb51.net, now to the domain name access, all redirected to Www.jb51.net; Just add the following code to the page where you need to turn:

Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
Response.Clear ();
Response.statuscode = 301;
Response.Status = "Moved Permanently";
Response.AddHeader ("Location", "http://www.jb51.net");
}

Using this method, the site will be the original domain name switch to the www.jb51.net, now the domain name should be very easy to remember.

I hope this article will help you with the ASP.net program design.

Related Article

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.