Nginx 301重定向設置二種方法

  進行了301重定向,把www.sunrisenan.com和sunrisenan.com合併,並把之前的域名也一併合併.

有兩種實現方法,第一種方法是判斷nginx核心變量host(老版本是http_host):

第一種方法:

server {

      server_name www.sunrisenan.com sunrisenan.com;

      if ($host != 'www.sunrisenan.com' ) {

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

      }

      ...

}


第二種方法:

 server {

if ($host = 'sunrisenan.com'){

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

        }

}

這兩種方法中, permanent是關鍵,詳細說明見nginx重定向規則說明。

  • last – 基本上都用這個Flag。

  • break – 中止Rewirte,不在繼續匹配

  • redirect – 返回臨時重定向的HTTP狀態302

  • permanent – 返回永久重定向的HTTP狀態301


測試是否定向成功

/usr/local/nginx/sbin/nginx -t

提示:

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

configuration file /usr/local/nginx/conf/nginx.conf test is successful


測試成功, 重啓nginx,輸入命令:

/usr/local/nginx/sbin/nginx -s reload


重啓之後測試一下~是否成功設定完成!

輸入命令:

curl  –I   sunrisenan.com

返回如下

HTTP/1.1 301 Moved Permanently

Server: nginx/0.7.65

Date: Tue, 03 Aug 2010 01:12:37 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

Location: http://www.sunrisenan.com/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章