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 on 301 to index.php, jbzj.com 301 to www.jbzj.com, otherwise go to error pageThe 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:<strong> </strong>www.jbzj.com");
}
else
{
Header("Location: /404.htm");
}
?>
Attach other Jump methods
The code is as follows:
/ / Define the code
Header( ‘Content-Type:text/html;charset=utf-8 ‘);
//Atom
Header(‘Content-type: application/atom+xml‘);
//CSS
Header(‘Content-type: text/css‘);
//Javascript
Header(‘Content-type: text/javascript‘);
//JPEG Image
Header(‘Content-type: image/jpeg‘);
//JSON
Header(‘Content-type: application/json‘);
//PDF
Header(‘Content-type: application/pdf‘);
//RSS
Header(‘Content-Type: application/rss+xml; charset=ISO-8859-1‘);
//Text (Plain)
Header(‘Content-type: text/plain‘);
//XML
Header(‘Content-type: text/xml‘);
// ok
Header(‘HTTP/1.1 200 OK‘);
//Set a 404 header:
Header(‘HTTP/1.1 404 Not Found‘);
/ / Set the address is permanently redirected
Header(‘HTTP/1.1 301 Moved Permanently‘);
//Go to a new address
Header(‘Location: http://www.example.org/‘);
/ / File delay steering:
Header(‘Refresh: 10; url=http://www.example.org/‘);
Print ‘You will be redirected in 10 seconds’;
// Of course, you can also use html syntax to achieve
// <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 content of the document has not changed.
Header(‘HTTP/1.1 304 Not Modified‘);
/ / Set the 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 the 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 the 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’;
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.
The above is, the PHP 301 redirect Jump data collation, hoping to help PHP development of small partners.
Source to: http://www.jb51.net/article/88785.htm
PHP Implementation 301 REDIRECT Jump instance Code