如何从服务器端设置域名不带www和带www的301重定向

手册/FAQ (469) 2016-04-09 10:39:22

如何从服务器端设置域名不带www和带www的301重定向

由于网站需要,把不带www和带www的域名合并到一起,而新网的又没有了转向功能,只能由服务器下手。谷歌知道需要使用301定向。以便将idcspy.org转向www.idcspy.org。

原来的配置文件如下

server

      {

          listen       80;

          server_name idcspy.org www.idcspy.org;

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

          root  /home/wwwroot/onelone.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;

      }

将idcspy.org删除再添加

server {

    server_name idcspy.org;

    rewrite ^(.*) https://www.idcspy.org$1 permanent;

}

由于最后就如下所示:

 

server

      {

          listen       80;

          server_name www.idcspy.org;

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

          root  /home/wwwroot/onelone.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 {

    server_name idcspy.org;

    rewrite ^(.*) https://www.idcspy.org$1 permanent;

}

 

THE END