Windows Virtual Host and VPS How to Achieve 301 Redirect (asp.net) _ Practical skills

Source: Internet
Author: User
Tags vps vps providers windows vps
301 redirects are so important, how do you achieve 301 redirects? Lou a variety of ways to implement 301 redirects in his article "detailed 301 permanent redirection implementations," but the approach is that for friends who use Windows virtual hosts or Windows VPS, in addition to a single page setting redirect, IIS The server realizes the whole station 301 redirects method but cannot apply. Because many of the virtual hosts and VPS providers do not support users to do 301 redirects. I encountered this problem, very troubled. Search a lot of information, or in the forum, well-known SEO blog inquiries, get the suggestion is: the virtual host is usually no way to do 301 Redirect, we recommend using a stand-alone server. Can have a stand-alone host, certainly good, but the silver is limited AH. I believe many friends have met the above problems.
After a period of research, I finally found the Windows Virtual Host and VPS implementation of 301 Redirect method, in this share with you:
1, the first way: through the Web.config configuration implementation (requires IIS must be version 7.0)
Suppose we need to redirect Jb51.net 301 to Www.jb51.net, then we add the following code to the <configuration> node in the Web.config file in the root directory of the program.
Copy Code code 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/{r:0}" redirecttype= "permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>

Unfortunately, many Windows Virtual host space with the IIS6.0, then IIS6.0 there is no way to achieve 301 redirect it? Please refer to the second method.
2, the second way: through the httpmodules URL interception implementation
We first add a new class library to the project, assuming the name is "Sitesense.domain". Add a "domainlocation" class under this class library and implement the IHttpModule interface with the following code:
Copy Code code 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.appsettings["Webdomain"]. ToString ();
String strweburl = configurationmanager.appsettings["Url301location"]. ToString ();
The URL that is intercepted does not contain "www.jb51.net" and contains "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: This class library must add references to "System.Configuration" and "system.web" namespaces.
Then we add the following code to the <configuration> node in the Web.config file in the root directory of the program
Copy Code code as follows:

<appSettings>
<add key= "Webdomain" value= "Jb51.net"/>
<add key= "url301location" value= "Www.jb51.net"/>
</appSettings>

in the
Copy Code code as follows:
<add name= "domainlocation" type= "SiteSense.Domain.DomainLocation, Sitesense.domain"/>

301 redirects can be achieved. When we are done, we can access jb51.net found that it has automatically changed to Www.jb51.net in the Explorer Bar. In order to confirm the success of 301 redirects, I developed a Web page to detect HTTP return status value of the tool, can be used to detect whether a Web site has done 301 redirect, the URL is: http://www.jb51.net/http_header/. The following figure is that I used the tool to detect after 301 redirects.

The above two methods of implementing 301 redirects are only suitable for asp.net programs and not for 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.