This article mainly introduces PHP implementation of 301 redirect jump, through the example code to let everyone better understand the redirection method, the need for small partners can refer to the next
In PHP 301 Redirection Implementation method is very simple we simply use the header to send 301 status code, and then use the header to jump, the effect and Apache,iis,nginx are the same effect oh.
One: This method is more recommended, because it can be http://www.jb51.net all the original URLs are transferred to the http://jb51.net new address
The code is as follows
<?php$the_host = $_server[' http_host '); $request _uri = Isset ($_server[' Request_uri '])? $_server[' Request_uri ': '; if ($the _host = = ' Www.jb51.net ') {header (' http/1.1 301 Moved permanently '); header (' Location:http://jb51.net '. $request _uri);//}?>
Two: Single-page multi-station Php301 redirect Code , Www.jb51.net and jb51.net 301 to index.php,jbzj.com 301 to Www.jbzj.com , otherwise go to the error page
The code is as follows
if (($HTTP _host== "www.jb51.net") or ($HTTP _host== "Jb51.net")) {header ("http/1.1 301 Moved permanently"); Header ("Location:/index.php");} ElseIf ($HTTP _host== "jbzj.com") {header ("http/1.1 301 Moved permanently"); Header ("Location:www.jbzj.com");} Else{header ("Location:/404.htm");}? >
Attach other Jump methods
The code is as follows
Define the coding header (' Content-type:text/html;charset=utf-8 ');//atomheader (' content-type:application/atom+xml ');// Cssheader (' content-type:text/css ');//javascriptheader (' content-type:text/javascript ');//JPEG Imageheader (' Content-type:image/jpeg ');//jsonheader (' Content-type:application/json ');//pdfheader (' content-type:application/ PDF ');//rssheader (' Content-type:application/rss+xml; charset=iso-8859-1 ');//text (Plain) header (' Content-type: Text/plain ');//xmlheader (' content-type:text/xml ');//Okheader (' http/1.1 OK ');//Set a 404 Header: Header (' http/1.1 404 Not Found ');//Set address is permanently redirected header (' http/1.1 301 Moved permanently ');//Go to a new address header (' location:http://www.example.org /');//File Delay Turn: header (' refresh:10; url=http://www.example.org/');p rint ' You'll be redirected in seconds ';//Of course, You can also use HTML syntax to implement//<meta http-equiv= "Refresh" content= "10;http://www.example.org//>//override x-powered-by:php: Header (' x-powered-by:php/4.4.0 '); header (' x-powered-by:brain/0.6b ');//Document Language header (' Content-language: En ');//Tell the browser the last modification time: (+)-60; or Filemtime ($FN), Etcheader (' last-modified: '. Gmdate (' d, D M Y h:i:s ', $time). ' GMT ');//Tell the browser that the document content has not changed header (' http/1.1 304 not Modified ');//Set Content Length header (' content-length:1234 ');// Set to a download type header (' Content-type:application/octet-stream '); header (' content-disposition:attachment; Filename= ') Example.zip "'); header (' content-transfer-encoding:binary ');//load the file to Send:readfile (' example.zip ');// Disables the cache header for the current document (' Cache-control:no-cache, No-store, max-age=0, Must-revalidate '); header (' Expires:mon, 1997 Jul 05:00:00 GMT '); Date in the Pastheader (' Pragma:no-cache ');//Set Content Type: Header (' content-type:text/html; charset=iso-8859-1 '); Header (' content-type:text/html; Charset=utf-8 '); header (' Content-type:text/plain '); Plain Text Format header (' Content-type:image/jpeg '); Jpg***header (' Content-type:application/zip '); Zip file header (' Content-type:application/pdf '); PDF file Header (' Content-type:audio/mpeg '); Audio file header (' Content-type:application/x-sHockw**e-flash '); Flash animation//Display Login Dialog header (' http/1.1 401 Unauthorized '); header (' Www-authenticate:basic realm= ' Top Secret "');p rint ' Text that would be displayed if the user hits cancel or ';p rint ' enters wrong login data ';
Jump to pay attention to the following points, to help solve some novice frequently encountered problems
1, location and ":" No space between, otherwise it will be wrong.
2, before using the header can not have any output.
3. The PHP code after the header will also be executed.
Summary: The above is the entire content of this article, I hope to be able to help you learn.