asp.net implement 301 redirect without www domain name to the domain name with www

Source: Internet
Author: User
Tags to domain

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 this article is very simple

Add 301 redirect instructions to the. htaccess file

The use of "mod_rewrite" technology, in the form of:

The code is as follows Copy Code

Rewriteengine on

Rewriterule ^ (. *) $ http://xxx.com/$1 [r=301,l]

1. redirect domain.com to www.domain.com

This redirect is designed to make the domain name unique, is the website SEO must do, the back redirects www.domain.com to domain.com is also for the same reason, but the form is different. Open the. htaccess file and add the following rules. (The following rule is for the primary domain name, and the subdomain is to be modified)

The code is as follows Copy Code

Rewriteengine on
Rewritecond%{http_host}!^www.domain.com$ [NC]
Rewriterule ^ (. *) $ http://www.domain.com/$1 [l,r=301]

2. redirect www.domain.com to domain.com

The code is as follows Copy Code

Rewriteengine on
Rewritecond%{http_host}!^domain.com$ [NC]
Rewriterule ^ (. *) $ http://domain.com/$1 [l,r=301]

3. REDIRECT olddomain.com to www.newdomain.com

The code is as follows Copy Code

Rewriteengine on
Rewritecond%{http_host}!olddomain.com$ [NC]
Rewriterule ^ (. *) $ http://www.newdomain.com/$1 [l,r=301]

4. redirect olddomain.com to newdomain.com

The code is as follows Copy Code

Rewriteengine on
Rewritebase/
Rewritecond%{http_host}!olddomain.com$ [NC]
Rewriterule ^ (. *) $ http://newdomain.com/$1 [l,r=301]

5. redirect domain.com/file/file.php to otherdomain.com/otherfile/other.php

The code is as follows Copy Code

Rewritecond%{http_host} ^www.domain.com$
Rewriterule ^file/file.php$ http://www.otherdomain.com/otherfile/other.php [r=301,l]


Implement 301 redirects with asp/php:

Asp:

The code is as follows Copy Code

Response.status= "Moved Permanently"

Response.AddHeader "Location", "http:///"

Response.End

Php:

  code is as follows copy code

Header (" http/1.1 moved Permanently ");

Header (" location:http:///");

Exit ();

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.