Home > Article > Backend Development > PHP Tutorial: How to Implement 301 Redirect_PHP Tutorial
What is 301 redirect? 301 redirection means that when your website address changes, such as changing domain names, website revisions, etc., and you want visitors or search engines to jump to the new address to visit the website, then you need to do 301 redirection. There are many ways to implement 301 redirection. This article mainly introduces how to implement 301 redirection in PHP programs.
Implement 301 redirect PHP code
$the_host = $_SERVER['HTTP_HOST']; //Mark to get the current domain name
$request_url = isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:''; //Determine the last part of the address
If($the_host !== ‘www.bkjia.com’) //There is no changed domain name address
{ header('HTTP/1.1 301 Moved Permanently'); // Prompt to issue 301
header('Location: http://www.bkjia.com/'.$request_url); //Add a new domain name address
}
?>
In which file to add the 301 redirect command
Add 301 redirect instructions to the .htaccess file and use "mod_rewrite" technology.
Example:
RewriteEngine on
RewriteRule ^(.*)$ http://www.bkjia.com/$1 [R=301,L]