apache+php Perfect solution to 301 redirects two ways _php skills

Source: Internet
Author: User
Tags addon domain
Fortunately, there are 301 redirects that can effectively solve such problems. As the Moonlight Blog said in this article,
301 redirect can promote search engine optimization effect
From the perspective of Search engine optimization, 301 redirect is the most feasible method of Web site redirection. When the site's domain name changes, the search engine will only index the new URL, but also the old address under the original external link to the new address, so that the site will not be ranked because of changes in the URL to receive the slightest impact. Similarly, when you use the 301 Permanent redirect command to point multiple domain names to the site's primary domain, there is no negative impact on the ranking of the site.

For more information on 301 redirects, you might as well Google it. This article only introduces the implementation method!
301 the implementation of redirection, I have also written a related article, but this article's solution is relatively simple, can only achieve the homepage of the jump, this article describes the two methods, you can achieve a perfect 301 redirect.

Method 1: Modify the. htaccess file
The code is as follows:
Copy Code code 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 Bo needs three domain names for redirection, so write more, the key code is 2 words
Copy Code code 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 the domain name of the website now.
Method 2: Use PHP's redirect code
Create a new index.php file, and then refer to the following code to make a simple change according to your redirection requirements:
Copy Code code 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 moved Permanently ');
Header ($location);
Exit ();
?>

If you redirect only one domain name, you can simplify the code into the following form:
Copy Code code as follows:

<?php
$the _host = $_server[' http_host '];//get into the domain name entered
$request _uri = isset ($_server[' Request_uri '))? $_server[' Request_uri ']: '//To determine the subsequent request section
if ($the _host!== ' jb51.net ')//jb51.net is my current domain name
{
Header (' http/1.1 moved Permanently ');/Send 301 Head
Header (' location:http://jb51.net '. $request _uri);//jump to my new domain address
Exit ();
}
?>

Note that the last exit () function must be written, I did not write at first, the results can only redirect the home page, such as Http://blog.iflyhigher.tk/guestbook Web pages, can not be redirected.
Finally, some details about redirects
Because to redirect three domain names, before redirecting, I first bind these three domain names to my server as addon domain, 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 the source: Gevin's 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.