The 301 redirect method in PHP is very simple. We just use the header to send the 301 status code.Then use the header to jump,The effect is the same as apache, iis, nginx.

code show as below

$the_host=$_server ["http_host"];
$request_uri=isset ($_ server ["request_uri"])?$_server ["request_uri"]:"";
header ("http/1.1 301 moved permanently");
header ("location:http://jb51.net". $request_uri);//

code show as below

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:<strong>
</strong>www.jbzj.com ");
header ("location:/404.htm");

Attach other jump methods

code show as below

//define encoding
header ("content-type:text/html;charset=utf-8");
header ("content-type:application/atom + xml");
header ("content-type:text/css");
header ("content-type:text/javascript");
//jpeg image
header ("content-type:image/jpeg");
header ("content-type:application/json");
header ("content-type:application/pdf");
header ("content-type:application/rss + xml;charset=iso-8859-1");
//text (plain)
header ("content-type:text/plain");
header ("content-type:text/xml");
header ("http/1.1 200 ok");
//Set a 404 header:
header ("http/1.1 404 not found");
//Set the address to be redirected permanently
header ("http/1.1 301 moved permanently");
//Go to a new address
header ("location:http://www.example.org/");
//File delayed turn:
header ("refresh:10;url=http://www.example.org/");
print "you will be redirected in 10 seconds";
//Of course, you can also use html syntax
//<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
$time=time ()-60;//or filemtime ($fn), etc
header ("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 as 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");
//Disable caching for the current document
header ("cache-control:no-cache, no-store, max-age=0, must-revalidate");
header ("expires:mon, 26 jul 1997 05:00:00 gmt");//date in the past
header ("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
//Show login dialog
header ("http/1.1 401 unauthorized");
header ("www-authenticate:basic realm =" top secret "");
print "text that will be displayed if the user hits cancel or";
print "enters wrong login data";

Note the following points when jumping,Helps solve some problems that novices often encounter

1. There must be no spaces between location and ":".Otherwise, an error will occur.

2. You cannot have any output before using the header.

3. The php code after the header will still be executed.

  • Previous Example analysis of cookie usage in Yii2
  • Next VS2015 Update2 build Android program summary