wordpress实现301重定向的方法

2014年7 月6日 / 网站源码 / 没有评论 / 480次

大家都知道301对SEO的意义,特别是首页有的站长喜欢将带www的地址直接跳转到不带www的域名地址,而又有的站长则喜欢网站地址都带上www。

今天与大家分享wordpress中如何去实现301的跳转。

1、使用代码实现

打开根目录下得wp-blog-header.php,在< ?php后面添加如下代码:

if(strtolower($_SERVER['SERVER_NAME'])!='填入需要跳转的域名'){
	$URIRedirect=$_SERVER['REQUEST_URI'];if(strtolower($URIRedirect)=="/index.php"){
		$URIRedirect="/";}
	header('HTTP/1.1 301 Moved Permanently');
	header('Location:填入需要跳转的域名'.$URIRedirect);exit();}

以上方法是所有域名重定向到不带“填入需要跳转的域名”

2、服务器中配置

a、Apache服务器可以修改.htaccess(默认已创建)文件

打开后可看到以下代码:

<IfModulemod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

从不带www重定向到带www添加如下301的配置代码即可:

RewriteEngine on
RewriteCond%{http_host}^填入不带www需要跳转的域名[NC]RewriteRule^(.*)$ 填入带www需要跳转的域名/$1 [L,R=301]

b、nginx添加301配置

在你nginx配置文件中的server模块配置添加如下代码:

server {
	server_name 填入带www需要跳转的域名 填入不带www需要跳转的域名';#301配置if($host !='填入需要跳转的域名'){
		rewrite ^/(.*)$ 填入需要跳转的域名/$1 permanent;}#其他的配置参数...}