Two Methods to Perfectly Implement 301 Redirects in Apache + PHP

Fortunately, 301 redirects can effectively solve this problem. As mentioned in the Moonlight Blog article,
301 redirects boost SEO performance.

From an SEO perspective, the 301 redirect is the most viable method for URL redirection. When a website’s domain name changes, search engines will only index the new URL while transferring all existing external links from the old address to the new one, so the site’s ranking is not affected at all by the domain change. Similarly, using a 301 permanent redirect command to point multiple domains to the main site domain will not have any negative impact on rankings.

For more details about 301 redirects, you can Google them. This article only covers the implementation methods!
I’ve previously written an article about implementing 301 redirects, but the solution in that article was relatively simple, only handling the homepage redirect. The two methods introduced in this article can perfectly implement 301 redirects.

Method 1: Modify the .htaccess File
The code is as follows:

Copy code The 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 three domains, so there’s more code, but the key part is just two lines:

Copy code The code is as follows:
RewriteCond %{HTTP_HOST} blog.iflyhigher.tk$ [NC]
RewriteRule ^(.*)$ http://blog.jb51.net/$1 [R=301,L]

The red domain is the old domain that needs to be redirected, and the green one is the current website domain.
Method 2: Use PHP Redirect Code
Create a new index.php file, then refer to the code below and make simple modifications according to your own redirect requirements:

Copy code The 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 one domain, you can simplify the code to the following form:

Copy code The code is as follows:
<?php
$the_host = $_SERVER['HTTP_HOST'];//Get the domain name entered
$request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';//Check the request URI part
if($the_host !== 'jb51.net')//jb51.net is my current domain
{
header('HTTP/1.1 301 Moved Permanently');//Send 301 header
header('Location: http://jb51.net'.$request_uri);//Redirect to my new domain address
exit();
}
?>

Note: the final exit() function is essential. I initially omitted it and could only redirect the homepage; pages like http://blog.iflyhigher.tk/guestbook would not redirect.
Finally, some details about the redirect
Since I needed to redirect three domains, I first bound these three domains as Addon Domains to my server and pointed all three to the same folder. This way, I only needed to modify the .htaccess file or index.php file in that one folder. If the .htaccess or index.php file doesn’t exist, simply create one.
I hope this article helps those who need to implement 301 redirects.
Please credit the source when reprinting: Gevin’s Blog

Leave a Comment

Your email address will not be published.