lnmp 301重定向

本站定了www.centoscn.com和centoscn.com两个域名,这种绑定对搜索引擎不友好,导致域名的PR值分散到两个URL上。那么如何解决呢?可以通过301重定向解决这个问题,把不带www和带www的域名合并到一起,指向同一个域名。这种301重定向在apache下可以通过.htaccess文档编辑相关命令。本博搭建的是LNMP环境,其中N是指nginx,在nginx下是无法通过.htaccess文档重定向的,需要通过rewrite 命令进行域名301重定向。

第一步:用WinSCP软件打开 /usr/local/nginx/conf/vhost/www.centoscn.com.conf(注:换成你服务器上的文件名称),进入/usr/local/nginx/conf/vhost/,点击www.centoscn.com.conf文件>>“右键”>>”编辑”,或下载到本地编辑。

打开之后大概显示以下内容:

server

{

listen 80;

server_name www.centoscn.com centoscn.com;

index index.html index.htm index.php default.html default.htm default.php;

root /home/wwwroot/www.centoscn.com;

include wordpress.conf;

location ~ .*\.(php|php5)?$

{

try_files $uri =404;

fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_index index.php;

include fcgi.conf;

}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 30d;

}

location ~ .*\.(js|css)?$

{

expires 12h;

}

access_log off;

}

第二步:删除“server_name www.centoscn.com centoscn.com;”中的“centoscn.com”,然后在所有代码最后添加以下代码:

server {

server_name centoscn.com;

rewrite ^(.*) http://www.centoscn.com$1 permanent;

}

第三步:重启nginx,使用Putty软件远程登陆服务器,使用命令/etc/init.d/nginx restart 或 /root/lnmp restart

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。