How to Implement 301 redirection for Windows virtual hosts and VPS (asp.net)

Source: Internet
Author: User
Tags vps providers windows vps

301 redirection is so important, how can we achieve 301 redirection? Lu songsong introduced a variety of methods to achieve 301 redirection in his article "details 301 permanent redirection implementation methods". However, the methods in this article are for friends who use Windows virtual hosts or Windows VPS, except for setting redirection for a single page, the IIS server cannot implement the 301 redirection for the whole site. Because many virtual hosts and VPS providers do not support 301 redirection by users. I have encountered this problem, which is very troublesome. I searched a lot of information or asked about it in a forum or a well-known SEO blog. The suggestion is that the VM generally cannot perform 301 redirection. We recommend that you use an independent server. I'm sure I can have an independent host, but I have limited money. I believe many of my friends have encountered the above problems.
After a period of research, I finally found a way to achieve 301 redirection between Windows virtual hosts and VPS. Here I will share with you:
1. Method 1: Use Web. config configuration (IIS must be version 7.0)
Suppose we need to redirect jb51.net 301 to www.jb51.net, then we can add the following code to the <configuration> node in the Web. config file under the root directory of the program. Copy codeThe Code is as follows: <system. webServer>
<Rewrite>
<Rules>
<Rule name = "Redirect" stopProcessing = "true">
<Match url = ". *"/>
<Conditions>
<Add input = "{HTTP_HOST}" pattern = "^ jb51.net $"/>
</Conditions>
<Action type = "Redirect" url = "http://www.jb51.net/javasr:0}" redirectType = "Permanent"/>
</Rule>
</Rules>
</Rewrite>
</System. webServer>

Unfortunately, IIS6.0 is used for many Windows virtual host spaces. Is there a way for IIS6.0 to implement 301 redirection? Refer to the second method.
2. Method 2: Implement HTTP modules URL blocking
First, we will add a new class library in the project, assuming the name is "SiteSense. Domain ". Add a "DomainLocation" Class under this class library and implement the IHttpModule interface. The Code is as follows:Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Web;
Using System. Configuration;
Namespace SiteSense. Domain
{
Public class DomainLocation: IHttpModule
{
Public void Dispose ()
{
}
Public void Init (HttpApplication context)
{
Context. AuthorizeRequest + = (new EventHandler (Process301 ));
}
Public void Process301 (object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender;
HttpRequest request = app. Context. Request;
String lRequestedPath = request. Url. DnsSafeHost. ToString ();
String strDomainURL = ConfigurationManager. receivettings ["WebDomain"]. ToString ();
String strWebURL = ConfigurationManager. receivettings ["URL301Location"]. ToString ();
// The intercepted Url does not contain "www.jb51.net", but "jb51.net"
If (lRequestedPath. IndexOf (strWebURL) =-1 & lRequestedPath. IndexOf (strDomainURL )! =-1)
{
App. Response. StatusCode = 301;
App. Response. AddHeader ("Location", lRequestedPath. Replace (lRequestedPath, "http: //" + strWebURL + request. RawUrl. ToString (). Trim ()));
App. Response. End ();
}
}
}
}

Note: The "System. Configuration" and "System. Web" namespaces must be referenced in such libraries.
Add the following code to the <configuration> node in the Web. config file under the root directory of the program:Copy codeThe Code is as follows: <etettings>
<Add key = "WebDomain" value = "jb51.net"/>
<Add key = "URL301Location" value = "www.jb51.net"/>
</AppSettings>

Add the following code to the Copy codeThe Code is as follows: <add name = "DomainLocation" type = "SiteSense. Domain. DomainLocation, SiteSense. Domain"/>

You can achieve 301 redirection. After that, we can access jb51.net and find that the browser bar has automatically changed to www.jb51.net. To confirm the success of 301 redirection, I developed a tool to detect the HTTP return status value of the web page, can be used to detect whether a web site has done 301 redirection, the web site is: http://www.jb51.net/http_header. Yes, I use this tool to detect 301 redirection.

The above two methods to achieve 301 redirection are only applicable to ASP. NET programs and not ASP programs.

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.