301 redirection promotes search engine optimization
From the perspective of search engine optimization, 301 redirection is the most feasible method for URL redirection. When the domain name of a website changes, the search engine only indexes the new website and transfers the original external links under the old address to the new one, in this way, the ranking of websites will not be affected by changes in the website. Similarly, when you use the 301 permanent redirection command to direct multiple domain names to the primary domain of the website, the ranking of the website will not have any negative impact.
For more information about 301 redirection, Google. This article only introduces the implementation method!
301. I have previously written an article about the implementation of redirection. However, the solution in this article is relatively simple and can only jump to the home page. The two methods described in this article are as follows, 301 redirection can be implemented perfectly.
Method 1: modify the. htaccess file
The code is as follows:
<Ifmodule mod_rewrite.c>
RewriteEngine On
RewriteCond % {HTTP_HOST} blog. iflyhigher. tk $ [NC]
RewriteRule ^ (. *) $ http://www.111cn.net/#1 [R = 301, L]
RewriteCond % {HTTP_HOST} iflyhigher. tk $ [NC]
RewriteRule ^ (. *) $ http: // http://www.111cn.net/#1 [R = 301, L]
RewriteCond % {HTTP_HOST} moiya. tk $ [NC]
RewriteRule ^ (. *) $ http: // http://www.111cn.net/#1 [R = 301, L]
</Ifmodule>
This blog needs to redirect the three domain names, so it writes a lot. The key code is two sentences.
RewriteCond % {HTTP_HOST} blog. iflyhigher. tk $ [NC]
RewriteRule ^ (. *) $ http://www.111cn.net/#1 [R = 301, L]
The red domain name is the old domain name to be redirected, and the green domain name is the current website domain name.
Method 2: use PHP redirection code
Create a new index. php tutorial file, and then follow the code below to make simple modifications according to your own redirection requirements:
<? Php
$ The_host = $ _ SERVER ['http _ host'];
$ Request_uri = isset ($ _ SERVER ['request _ URI '])? $ _ SERVER ['request _ URI ']: '';
Switch ($ the_host)
{
Case "www. iflyhigher. tk ":
Case "iflyhigher. tk ":
$ Location = "Location: http: // http://www.111cn.net". $ request_uri;
Break;
Case "blog. iflyhigher. tk ":
$ Location = "Location: http://www.111cn.net". $ request_uri;
Break;
Case "www. moiya. tk ":
Case "moiya. tk ":
$ Location = "Location: http: // http://www.111cn.net ";
Break;
Default:
$ Location = "Location: http: // http://www.111cn.net ";
Break;
}
Header ('http/1.1 301 Moved Permanently ');
Header ($ location );
Exit ();
?>
If you only need to redirect a domain name, you can simplify the code into the following form:
<? Php
$ The_host = $ _ SERVER ['http _ host']; // Obtain the entered domain name
$ Request_uri = isset ($ _ SERVER ['request _ URI '])? $ _ SERVER ['request _ URI ']: ''; // you can specify the REQUEST
If ($ the_host! = 'Http: // www.111cn.net ') // The http://www.111cn.net is my current domain name
{
Header ('http/1.1 301 Moved Permanently '); // issue the 301 header
Header ('Location: http: // http://www.111cn.net '. $ request_uri); // jump to my new domain address
Exit ();
}
?>
Note that the last exit () function must be written. I didn't write it at first, and the result can only be redirected to the home page, such as http://blog.iflyhigher.tk/guestbook.
Finally, some details about redirection
As we need to redirect the three Domain names, before redirection, I first bind these three Domain names to my server as Addon Domain and direct these three Domain names to the same folder, in this way, you only need to modify. htaccess file or index. php file. If there is no. htaccess file or index. Php file, create a new one.
I hope this article will help anyone who needs 301 redirection.
1. Redirect and RedirectMatch commands of the Apache module mod_alias
The two commands mentioned above use similar methods. The difference is that the latter RedirectMatch sends an external redirection syntax to the current URL based on regular expression matching:
Redirect [status] URL-path URL
RedirectMatch [status] regex URL
The status parameter can use the following HTTP status code:
Permanent
A permanent redirect status code (301) is returned, indicating that the location change of the resource is permanent.
Temp
Returns a temporary redirect status code (302), which is the default value.
Seeother
A "see" status code (303) is returned, indicating that the resource has been replaced.
Gone
An "obsolete" status code (410) is returned, indicating that the resource has been permanently deleted. If this status code is specified, the URL parameter is ignored.
Example:
APACHE
Redirect 301/old/old.htm http://www.baidu.com/new.htm
Redirect permanent/one http://baidu.com/two
RedirectMatch 301 (. * pai.gif $ yun_qi_img/error.html
2. Use mod_rewrite to rewrite the URL
APACHE
Options + FollowSymLinks
RewriteEngine on
RewriteCond % {HTTP_HOST} ^ baidu.com
RewriteRule ^ (. *) $ http://www.baidu.com/#1 [R = permanent, L]