Asp.net implements 301 redirection without www domain name to domain name with www

Source: Internet
Author: User
Tags subdomain name to domain

This article introduces asp.net's implementation of 301 redirection without www domain name to domain name with www. It also includes various implementation methods, including php, asp, and apache.

Some friends may use the benzene method, and add the Response. RedirectPermanent method to each Controller. A simple practice is to add the following code to 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: // bKjia. c0m "))
{
Response. RedirectPermanent ("http://www.bKjia. c0m"); // not good
}
}

If you are serious, you will find that this writing method is somewhat unfriendly and the following writing method looks better:

The Code is as follows: Copy code

Response. RedirectPermanent (strUrl. Replace ("http: // bKjia. c0m", "http://www.bKjia. c0m "));

// Good. This article is very simple.

Add the 301 redirection command to the. htaccess file.

The "mod_rewrite" technology is used, for example:

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 type of redirection aims to make the domain name unique and is required by the website SEO. The redirection from 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 rules apply to the primary domain name. The subdomain name must 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]


Use ASP/PHP to implement 301 redirection:

ASP:

The Code is as follows: Copy code

Response. Status = "301 Moved Permanently"

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

Response. End

 

PHP:

The Code is as follows: Copy code

Header ("HTTP/1.1 301 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.