logo

301重定向设置成功

经过我不懈的寻找,终于找到了想要的代码,现在我所有的不带WWW的网址,全部转到了WWW开头的网址。代码分享下:

if (strtolower($_SERVER[‘SERVER_NAME’])!=’www.ilouhao.com’){
$URIRedirect=$_SERVER[‘REQUEST_URI’];
if(strtolower($URIRedirect)==”/index.php”){
$URIRedirect=”/”;
}
header(‘HTTP/1.1 301 Moved Permanently’);
header(‘Location:http://www.ilouhao.com’.$URIRedirect);
exit();
}

这段代码是加在头部文件,title后面头一个<?php标签里面,我的头部代码全部都贴出来,方便查看添加到位置。使用的时候把上面那部分代码,里面的域名,换成你的。

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml” <?php language_attributes(‘xhtml’); ?>>
<head>
<META name=”y_key” content=”4d55976f3b4e2192″ />
<META name=”y_key” content=”96b2c4421974900b” />
<meta http-equiv=”Content-Type” content=”<?php bloginfo(‘html_type’); ?>; charset=<?php bloginfo(‘charset’); ?>” />
<meta http-equiv=”X-UA-Compatible” content=”IE=edge” />

<title><?php
if (strtolower($_SERVER[‘SERVER_NAME’])!=’www.ilouhao.com’){
$URIRedirect=$_SERVER[‘REQUEST_URI’];
if(strtolower($URIRedirect)==”/index.php”){
$URIRedirect=”/”;
}
header(‘HTTP/1.1 301 Moved Permanently’);
header(‘Location:http://www.ilouhao.com’.$URIRedirect);
exit();
}

 /*
* Print the <title> tag based on what is being viewed.
*/
global $page, $paged;

 wp_title( ‘|’, true, ‘right’ );

 // Add the blog name.
bloginfo( ‘name’ );

 // Add the blog description for the home/front page.
$site_description = get_bloginfo( ‘description’, ‘display’ );
if ( $site_description && ( is_home() || is_front_page() ) )
echo “-河南郑州景安网络数据中心|双线BGP服务器租用托管|备案|VPS|电话”;

 // Add a page number if necessary:
if ( $paged >= 2 || $page >= 2 )
echo ‘ | ‘ . sprintf( __( ‘Page %s’, ‘twentyten’ ), max( $paged, $page ) );

 ?></title> 

<meta name=”keywords” content=”河南郑州景安服务器托管租用价格,备案,娄昊,河南多线BGP机房,河南双线BGP机房,河南双线BGP双线机房租用托管”/>
<meta name=”description” content=”景安商务娄昊的博客,服务器安全,备案,开源,SEO,多线租用、托管,个人日志。电话:13633819762 QQ:461606413 537008023″/>

<link rel=”stylesheet” type=”text/css” href=”<?php echo get_stylesheet_directory_uri(); _e(‘/style.css’, THEME_NAME); ?>” media=”screen” />
<!–[if IE]><link rel=”stylesheet” type=”text/css” href=”<?php echo get_template_directory_uri(); ?>/ie.css” media=”screen” /><![endif]–>
<?php wp_head(); ?>
<script type=”text/javascript”>

  var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-23083678-3’]);
_gaq.push([‘_trackPageview’]);

  (function() {
var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
})();

</script>
<meta name=”chinaz-site-verification” content=”50fafb64-50a4-4261-9d9c-c4d66310e0d9″ />
</head>
<body <?php body_class(); ?>>

<div id=”page”>
<div id=”header”>
<h1 id=”blog-name”><a href=”<?php echo home_url(); ?>”><?php bloginfo(‘name’); ?></a></h1>
<div id=”blog-description”><?php bloginfo(‘description’); ?></div>
<div><?php
// added by <yourname> in order to add the slideshow using the frontpage-slideshow plugin echo do_shortcode(‘[FrontpageSlideshow]’);
?>
<?php
$options = &$GLOBALS[‘simpledark_options’];
$menu_args = ‘echo=0&depth=2&container=div&container_class=top-menu-window&theme_location=top-nav&fallback_cb=simpledark_menu’;
if($options[‘top_menu_show_home’]) {
$menu_args = $menu_args . ‘&show_home=1’;
}
echo function_exists(‘wp_nav_menu’) ? wp_nav_menu($menu_args) : simpledark_menu($menu_args);
?>


</div>
</div>
<div id=”main”>
<div id=”content”>

重定向到效果已经测试过了,是成功的,但是我把这个代码交给一个朋友测试的时候,却说不能用,不知道是代码搞错了,或是和什么代码冲突了,如果是默认主题修改的模板,应该没问题吧。

Leave your needs and contact information