前几天对本博客的wordpress文章固定链接进行了更改.为了防止流量的流失,使用了php下的301重定向.wordpress本身就是php,这个简单方便,很使用.
个人原创,版权所有,转载请注明原文出处:
http://www.embbnux.com/2014/04/05/wordpress_php_to_301_redirect/
php下的重定向还是很简单的,找到wordpress根目录下的: index.php
在最顶部添加下面的代码:
<?php
$the_host = $_SERVER['HTTP_HOST'];
$URIRedirect=$_SERVER['REQUEST_URI'];
if(strtolower($URIRedirect)=="/2014/03/24/263"){
$URIRedirect="/2014/03/24/on_ubuntu_use_vnc_connect_raspberry/";
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.embbnux.com$URIRedirect");
exit();
}
?>
这样就会自动重定向站内链接到另一个站内链接,
如果要进行重定向到站外链接,比如你的博客更换了域名,那就使用代码:
<?php
$the_host = $_SERVER['HTTP_HOST'];
if(strtolower($the_host) != 'www.embbnux.com')
{
$URIRedirect=$_SERVER['REQUEST_URI'];
if(strtolower($URIRedirect)=="/index.php")
{
$URIRedirect="/";
}
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.embbnux.com$URIRedirect");
exit();
}
?>
把这个放到原来域名里面的index.php