Apache + php provides two perfect solutions for 301 redirection

Source: Internet
Author: User
Tags addon domain

Fortunately, 301 redirection can effectively solve this problem. As described in the moonlight blog,
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:Copy codeThe Code is as follows: <ifmodule mod_rewrite.c>
RewriteEngine On
RewriteCond % {HTTP_HOST} blog. iflyhigher. tk $ [NC]
RewriteRule ^ (. *) $ http://blog.jb51.net/#1 [R = 301, L]
RewriteCond % {HTTP_HOST} iflyhigher. tk $ [NC]
RewriteRule ^ (. *) $ http://jb51.net/#1 [R = 301, L]
RewriteCond % {HTTP_HOST} moiya. tk $ [NC]
RewriteRule ^ (. *) $ http://jb51.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.Copy codeThe Code is as follows: RewriteCond % {HTTP_HOST} blog. iflyhigher. tk $ [NC]
RewriteRule ^ (. *) $ http://blog.jb51.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 an index. php file, and then follow the code below to make simple modifications according to your own redirection requirements:Copy codeThe Code is as follows: <? 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://jb51.net". $ request_uri;
Break;
Case "blog. iflyhigher. tk ":
$ Location = "Location: http://blog.jb51.net". $ request_uri;
Break;
Case "www. moiya. tk ":
Case "moiya. tk ":
$ Location = "Location: http://jb51.net ";
Break;
Default:
$ Location = "Location: http://jb51.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:Copy codeThe Code is as follows: <? 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! = 'Jb51. net') // jb51.net is my current domain name
{
Header ('HTTP/1.1 301 Moved Permanently '); // issue the 301 header
Header ('location: http://jb51.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.
Reprinted to indicate the source: Gevin blog

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.