Php implements 301 redirection jump instance code and 301 redirection

Source: Internet
Author: User

Php implements 301 redirection jump instance code and 301 redirection

In php, the 301 redirection implementation method is very simple. We only need to simply use the header to send the 301 status code and then use the header to jump. The effect is the same as that of apache, iis, and nginx.

I: This method is more recommended, because it can convert all the original URLs of the http://www.bkjia.com to the new address of the http://jb51.net

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);//}?>

2. Single-page multi-site Php301 redirection code, www.jb51.net and jb51.net are 301 to index. php, and jbzj.com is 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 redirection Methods

The Code is as follows:

// Define the encoding headers ('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'); // Response Header ('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 200 OK '); // set a 404 header: header ('HTTP/1.1 404 Not Found '); // set the address to be Permanently redirected header ('HTTP/1.1 301 Moved Permanently '); // go to a new address header ('location: http://www.example.org/'); // file delay redirection: 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 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-: brain/0.6b '); // the header of the Document language ('content-language: en'); // notify the browser of the last modification time $ time = 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 the header ('HTTP/1.1 304 Not modified'); // set the Content Length header ('content-Length: 1234 '); // set it 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 '); // disable the Cache header ('cache-Control: no-cache, no-store, max-age = 0, must-revalidate') for the current document; header ('expires: mon, 26 Jul 1997 05:00:00 gmt'); // Date in the pastheader ('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 Box 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 ';

Pay attention to the following points to help solve some problems that beginners often encounter.

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

2. There cannot be any output before using the header.

3. the PHP code after the header is executed.

The above is the sorting of php 301 redirection materials, hoping to help php developers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.