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 (); |