Home  >  Article  >  Backend Development  >  How to implement 301 redirect in php

How to implement 301 redirect in php

WBOY
WBOYOriginal
2016-07-25 09:03:371014browse
  1. $the_host = $_SERVER['HTTP_HOST']; //Mark to get the current domain name
  2. $request_url = isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']: ''; //Determine the last part of the address
  3. if($the_host !== 'bbs.it-home.org') //The domain name address that has not been changed
  4. {
  5. header('HTTP/1.1 301 Moved Permanently'); / / Prompt to issue 301
  6. header('Location: http://bbs.it-home.org/'.$request_url); //Add new domain name address
  7. }
  8. ?>
Copy code

2. In which file should you add the 301 redirect command Add 301 redirect instructions to the .htaccess file and use mod_rewrite (url rewriting) technology. Example:

  1. RewriteEngine on
  2. RewriteRule ^(.*)$ http://bbs.it-home.org/$1 [R=301,L]
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn