asp.net MVC 3 301 permanent redirect without WWW domain name to domain with www

Source: Internet
Author: User
Tags to domain

Friends who use ASP.net and follow the new technology may already know that the Response.redirectpermanent () method has been added in ASP.net 4.0 to achieve permanent redirection, and the effect of the method is clearly explained in the annotation: execution from the requested URL to the specified A permanent redirection of URLs and provides options for completing the response.

In the ASP.net MVC 3 project, we can use the following notation to implement permanent redirection as needed:

The code is as follows Copy Code

Public ActionResult Index ()
{
Return redirectpermanent ("contact");
Return redirecttoactionpermanent ("contact");
Return redirecttoroutepermanent ("contact");
}

We used to be able to set the status code in the HTTP data stream returned by the server, such as specifying a status code of 301 for permanent redirection, and now in asp.net 4.0, asp.net Web forms projects and ASP.net MVC projects, we can easily implement URLs permanently redirect.

The implementation of the domain name from the WWW to the permanent redirect to the domain name without www, the webmaster to do so is to improve the ranking of domain names.

Some friends may use the method of comparing benzene, add Response.redirectpermanent method under each Controller. The simpler approach is to add the following code to the Global.axax.cs:

The code is as follows Copy Code
protected void Application_BeginRequest (object sender, EventArgs e)
{
String strurl = Request.Url.ToString (). Trim (). ToLower ();
if (Strurl.contains ("Http://111cn.net"))
{
Response.redirectpermanent ("Http://www.111cn.net"); Not good
}
}

If you are serious, you will find that the wording is somewhat unfriendly, and the following wording is better:

The code is as follows Copy Code

Response.redirectpermanent (Strurl.replace ("Http://111cn.net", "http://www.111cn.net")); Good


The following days tell you how to force a user or seo/seo.html "target=" _blank > Search engine Redirect to a unique domain name in. htaccess.
. The code is as follows:

The code is as follows Copy Code

#强制重定向到www
Rewriteengine on
Rewritecond%{http_host} ^111cn.net/[NC]
Rewriterule ^ (. *) $ http://www.111cn.net/$1 [l,r=301]

#强制重定向到不带www的顶级域名
Rewriteengine on
Rewritecond%{http_host}!^111cn.net/$ [NC]
Rewriterule ^ (. *) $ http://111cn.net/$1 [l,r=301]

iis301 is very simple. Just create two sites and then use one to jump to another to configure without introducing.

This article is very simple, but many people in the process of permanent redirection is wrong, I hope to help

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.