IIS下PHP实现的404页301跳转

是不是看不懂了?

那么这样说吧.比如1ting.com是个IIS下的独立站点,和www.1ting.com是不同的.而且1ting.com没有rewrite之类的,只能运行php.那么怎么实现访问1ting.com/xxx.html时候直接跳转到www.1ting.com/xxx.html.光总想了一个办法:自定义404页面,然后用404页面来301转向.

先指定一个404页面

404页面的代码:
<?php
$request_uri = isset($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : '';
$redirect_uri = sprintf('http://www.1ting.com%s',$request_uri);
header( "HTTP/1.1 301 Moved Permanently" );
header( "content-type: text/html;charset=UTF-8" );
header('Status: 301 Moved Permanently');
header(sprintf('Location: %s',$redirect_uri) );
echo ('<a href="' .$redirect_uri . '">The page you are looking for has moved to ' .$redirect_uri . '</a>');
?>

用这段代码就能实现了.当然还可以重定向到别的上去.但是返回的状态码是404先.
还有,别他妈跟我讲SEO了,这个是给人看的.

2 Responses to IIS下PHP实现的404页301跳转

  1. 还有,别他妈跟我讲SEO了,这个是给人看的.

    将的好

  2. [...] 之前发过IIS下PHP实现的404页301跳转来实现http://old.com/r.html301转到http://new.com/r.html,今天发现是舍近求远了.利用IIS自己功能就可以完全实现301转向到特定URL,不用写任何东西. 打开IIS上站点的属性,切换到主目录选项卡下. 选择重定向到URL 然后勾选资源永久重定向(必须选,这样才能返回301状态,302对搜索引擎不友好) 然后勾选最上面的上面输入的准确URL 接着在重定向到那里输入目标URL,比如http://www.new.com$S$Q (注意:后面写上变量$S$Q,具体Redirect Reference可以看微软的文档) 点确定即可. [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>