本站绑定了59vps.com和www.59vps.com,这样对搜索引擎很不友好,导致域名的PR值分散到两个URL上了。301重定向就可以解决这个问题,把不带www和带www的域名合并到一起。下面以本站为例,做了LNMP环境下的nginx域名301重定向教程。

直接用nano编辑器打开 /usr/local/nginx/conf/vhost/59vps.com.conf 这个配置文件,代码如下:

server
      {
          listen       80;
          server_name 59vps.com www.59vps.com;
          index index.html index.htm index.php default.html default.htm default.php;
          root  /home/wwwroot/59vps.com;

          include typecho.conf;
          location ~ .*\.(php|php5)?$
                  {
                          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 59vps.com www.59vps.com; 这行的 59vps.com 删掉。再在后面加上下面代码:

server {
    server_name 59vps.com;
    rewrite ^(.*) http://www.59vps.com$1 permanent;
}

  最后的效果图如下:

 

改完后重启nginx就生效了。虽然做了301重定向,但是DNS处还是要做A记录解析的。完成后,试试效果,打开59vps.com就会自动跳转到www.59vps.com

 

Last modification:July 14, 2020
如果觉得我的文章对你有用,请随意赞赏