2015-06-12 44 views
-1

我需要一個基本的重定向規則,所以如果有人輸入:www.site.com將被重定向到site.com。我需要它在瀏覽器中看起來乾淨整潔。NginX重寫/重定向規則http:// www到http://

我在nginx的serverblock嘗試這樣做:

# trying redirects from http:// www.site.com to http:// site.com 
if ($host = "www.site.com") { 
rewrite^$scheme://site.com$uri permanent; 
} 

... 

location/{ 
    try_files $uri $uri/ @modx-rewrite /index.php?/$request_uri; 
    location ~ .php$ { 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_index index.php; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    include fastcgi_params; 
    } 

但出於某種原因,沒有工作,我得到「未找到錯誤的服務器」。 而且它還在我的地址中添加了http S。

如果沒有這些規則,我的網站可以正常打開並通過https運行,並且網頁通常可以通過https:// site.com鏈接打開。

謝謝。

注意:我故意在代碼snipets地址中添加了幾個空格,否則stackoverflow不會讓我發佈它。

回答

1

使其更簡單:

server { 
    listen  80; 
    server_name example.com; 
    location/{ .........; } 
} 
server { 
    listen  80; 
    server_name www.example.com; 
    location/{ return 301 http://example.com$request_uri; } 
}