apache+php Perfect solution to 301 redirects in two ways _php tutorial

Source: Internet
Author: User
Tags addon domain
Fortunately, there are 301 redirects that can solve this problem effectively. As the Moonlight blog post says,
301 redirects to boost search engine optimization results
From the perspective of SEO, 301 redirection is the most feasible way to redirect URLs. When the domain name of the site changes, the search engine will only index the new URLs, but also the old address under the original external links to the new address, so that the site will not be ranked because of the change in the slightest impact. Similarly, when you use the 301 Permanent redirect command to point multiple domain names to the site's home domain, there is no negative impact on the site's ranking.

For more on the 301 redirect, you might want to Google a bit. This article only describes the implementation method!
301 REDIRECT Implementation, I have also written a related article, but this article's solution is relatively simple, only to achieve the main page of the jump, the two methods described in this article, can be perfect to achieve 301 redirect.

Method 1: Modify the. htaccess file
The code is as follows:
Copy CodeThe code is as follows:

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]


This blog needs to redirect three domain names, so write more, the key code is 2 words
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 that needs to be redirected, the green is now the domain name of the website.
Method 2: Use the redirect code for PHP
Create a new index.php file, and then refer to the following code to make simple changes to your own redirection requirements:
Copy CodeThe code is as follows:
$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 redirect only one domain name, you can simplify the code to the following form:
Copy CodeThe code is as follows:
$the _host = $_server[' http_host '];//get into the domain name entered
$request _uri = isset ($_server[' Request_uri ')? $_server[' Request_uri ': ';//Judge the part of the request behind
if ($the _host!== ' jb51.net ')//jb51.net is my domain now
{
Header (' http/1.1 301 Moved permanently ');//301 head issued
Header (' location:http://jb51.net '. $request _uri);//jump to my new domain address
Exit ();
}
?>

Note that the last exit () function is certain to write, I did not write initially, the result can only redirect the homepage, such as Http://blog.iflyhigher.tk/guestbook Web page, can not be redirected.
Finally, some of the details about redirection
In order to redirect three domain names, before redirecting, I first bind these three domain names as addon domain to my server, and let these three domains point to the same folder, so that Just modify the. htaccess file or the index.php file in this folder. If you do not have a. htaccess file or a index.php file, create a new one.
I hope this article is helpful to friends who need to make 301 redirects.
Reprint please indicate source: Gevin's Blog

http://www.bkjia.com/PHPjc/323597.html www.bkjia.com true http://www.bkjia.com/PHPjc/323597.html techarticle Fortunately, there are 301 redirects that can solve this problem effectively. As the Moonlight Blog article said, 301 redirects can promote search engine optimization from the perspective of SEO, 3 ...

  • 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.