suchen
Heimphp教程php手册php实现301永久重定向和302临时重定向方法

在服务器中301与302对于搜索引擎来讲一个是永久的跳新的地址了,一个是告诉你暂时到了一个新地址了,那么我们在php中怎么实现301永久重定向和302临时重定向呢,下面我们一起来看看方法的实现程序.

实现重定向的原理很简单,就是Web服务器返回个HTTP header给浏访问者,PHP发送HTTP header这个功能是由header()函数来实现的,301,302,404 这些状态码是在HTTP协议中约定好的,所以不用打破沙锅问"为什么是301而不是3001”,扯多了,回到正题.

PHP 301重定向,代码如下:

header('HTTP/1.1 301 Moved Permanently'); 
Header( "Location: http://www.phprm.com/" );  
exit();

//或者如下代码  

<?php 
//301永久重定向 
$http_protocol = $_SERVER[&#39;SERVER_PROTOCOL&#39;];   //http协议版本 
//如果是其他协议,则默认为HTTP/1.0 
if ( &#39;HTTP/1.1&#39; != $http_protocol && &#39;HTTP/1.0&#39; != $http_protocol )
$http_protocol = &#39;HTTP/1.0&#39;; 
//响应301状态码 
header("$http_protocol 301 Moved Permanently"); 
//指定重定向的URL 
$new_url = &#39;http://www.phprm.com/&#39;; 
header("Location:$new_url"); 
?>

PHP 302重定向,代码如下:

header("Location: http://www.phprm.com/");  
exit();

顺到把PHP 404错误也附带上,代码如下:

header("HTTP/1.1 404 Not Found"); 
exit();

这里关于php 301与302重定向就讲到了这里了,下面附一下apache做法,例:

APACHE代码如下:

Redirect 301 /old/old.htm http://www.phprm.com/new.htm 
Redirect permanent /one http://phprm.com/two 
RedirectMatch 301 (.*).gif$ http://www.phprm.com/images/$1.jpg

使用mod_rewrite重写URL方式

APACHE代码如下:

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^phprm.com 
RewriteRule ^(.*)$ http://www.phprm.com/$1 [R=permanent,L]

关于apache htaccess这里就不介绍人了与mod_rewrite重写URL方式几乎是完全一样.


永久地址:

转载随意~请带上教程地址吧^^

Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

SublimeText3 Linux neue Version

SublimeText3 Linux neue Version

SublimeText3 Linux neueste Version

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools