说明:
使用header()函数同时301和重定向。
代码:
<?php // 说明:301重定向 // 补充:输出两个header就可以啦 header( "HTTP/1.1 301 Moved Permanently" ); header( "Location: /" ); ?>
例子:
<?php // 如果通过“upall.cn”或“www.upall.cn”访问的话重定向到“blog.upall.cn” $hostFrom = $_SERVER['HTTP_HOST']; $hostTo = 'blog.upall.cn'; $hosts = array( 'upall.cn', 'www.upall.cn', ); if (in_array($hostFrom, $hosts)){ $queryString = $_SERVER['QUERY_STRING']; $scriptName = $_SERVER['SCRIPT_NAME']; if (!empty($queryString)){ $queryString = '?' . $queryString; } $url = $hostTo.$scriptName.$queryString; header('HTTP/1.1 301 Moved Permanently'); header("Location: http://$url"); } ?>
Apache版跳转
<VirtualHost *:80> ServerName www.baidu.com ServerAlias baidu.com Redirect / http://baidu.com.demo.upall.cn/ </VirtualHost>
.htaccess 跳转
不带www网址的跳转到带www的网址上
RewriteEngine on RewriteCond %{HTTP_HOST} ^upall.cn [NC] RewriteRule /^(.*)$ http://www.upall.cn/$1 [L,R=301]
<完。作者:upall>
更多语言的301重定向:
http://www.webconfs.com/how-to-redirect-a-webpage.php