一、PHP
重定向单个网页
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.newdomain.com/page.html");
exit();
?>
重定向多个网页
<?php
if (substr($_SERVER['HTTP_HOST'],0,3) != 'www') {
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.'.$_SERVER['HTTP_HOST']
.$_SERVER['REQUEST_URI']);
}
?>
二、.htaccess文件
重定向单个网页
Redirect 301 /old/oldpage.htm /new/http://www.domain.com/newpage.htm
重定向多个网页
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
三、ASP
重定向单个网页
<% Response.Status="301 Moved Permanently" Response.AddHeader='Location','http://www.newdomain.com/' %>
重定向多个网页
<%
If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www."
& Request.ServerVariables("HTTP_HOST")
& Request.ServerVariables("SCRIPT_NAME")
End if
%>
301重定向对用户体验会不会有问题?
一般来讲不会的,重定向可以用在以下几种情况下:
1. 重定向老站点到新站点
2. 多个域名指向一个站点
3. 显示或者不显示www
4. 调整URL的结构