The code for implementing 301 redirection for various program systems is described as follows: 1. Linux host redirects to Godaddy's Liunx host. Godaddy itself supports Apache, so you can directly create one. the htaccess file is enough. generally, it cannot be created locally. when using htaccess, you can first create a txt file and then rename it as & ldquo; when uploading it to the root directory ;. htaccess & rdquo; describes the code for implementing 301 redirection for various program systems:
1. Linux host redirection
Godaddy's Liunx host, Godaddy itself supports Apache, so you can directly create. the htaccess file is enough. generally, it cannot be created locally. when using htaccess, you can create a txt file and rename it to ". htaccess. There are many tutorials on how to perform 301 redirection on the Internet, whether it is full-site redirection or single-page redirection. The following uses my www.wecanwecan.com as an example.
1.1 Transfer without www domain name to www domain name
RewriteEngine on
Rewritecond % {http_host} ^ wecanwecan.com [nc]
Rewriterule ^ (. *) $ http://www.wecanwecan.com/#1 [r = 301, nc]
1.2 whole site 301 redirection
Options + FollowSymlinks
RewriteEngine on
RewriteCond % {HTTP_HOST} ^ wecanwecan.com [NC]
RewriteRule ^ (. *) $ http://www.wecanwecan.com/#1 [L, R = 301]
RewriteCond % {HTTP_HOST} ^ www.wecanwecan.com [NC]
RewriteRule ^ (. *) $ http://wecancan.com/#1 [L, R = 301]
The other is in index. php under the root directory.
Header ("HTTP/1.1 301 Moved Permanently ");
Header ("Location: http://wecanwecan.com /");
Exit ();
2. ASP host 301 redirection
Add the following lines to the top of index. asp or default. asp:
The code is as follows:
<%
Response. Status = "301 Moved Permanently"
Response. AddHeader "Location", "www.wecanwecan.com"
Response. End
%>
3. ASP.net host redirection 301
ASP. NET:
Response. Status = "301 Moved Permanently ";
Response. AddHeader ("Location", "http://www.wecanwecan.com ");
}
I encapsulate it in a class:
Using System; using System. collections. generic; using System. text; using System. web. UI; using System. web. UI. htmlControls; namespace ClassLib {public class URLClass {private bool flag301 = false; // whether to enable 301 private bool isIndex = false; // whether to return to the home page or retain it on the current page ////// Constructor //////Start 301?///Page///Format: www.xxx.comPublic URLClass (bool fl, Page, string strURL) {flag301 = fl; URL301 (page, strURL );}////// Return to the home page /////////Format: www.xxx.comPublic void URL301 (Page page, string strURL) {// 301 redirect if (page. Request. Url. DnsSafeHost! = StrURL & flag301 = true) {page. response. clear (); page. response. status Code = 301; page. response. status = "301 MovedPermanently"; page. response. addHeader ("Location", "http: //" + strURL); page. response. end ();}}}}
12 3 Next page