2012-11-20 104 views
0

我的Nginx設置目前有這樣的:Nginx的重定向設置

location/{ 
     if (!-e $request_filename){ 
      rewrite ^/(.*)$ https://domain.com/index.php?id=$1 redirect; 
     } 
} 

基本上是不存在的網頁(404),它重定向用戶到主頁。但現在我有一個WordPress的博客設置https://domain.com/blog/,但任何wordpress項目,例如。 https://domain.com/blog/test也被重定向到主頁。我想知道如何解決這個問題?

回答

0

做的請求的URL的,其路徑的開頭「/博客/」添加相應的位置,像這樣不同的東西:如果

location/{ 
    if (!-e $request_filename){ 
    rewrite ^/(.*)$ https://domain.com/index.php?id=$1 redirect; } 
} 


location /blog/ { 
    #add in whatever directives are needed to serve your wordpress 
} 
0

您不應該使用if。請閱讀nginx wiki上的IfIsEvil頁面。相反,你應該使用try_files

你的配置應該看起來更像是這樣的:

location/{ 
    try_files $uri $uri/ @notfound 
} 

location @notfound { 
    rewrite ^(.*)$ https://domain.com/index.php?id=$1 redirect; 
} 

真的,你不應該在所有這樣做。相反,你應該設置一個custom error頁面。將每404重定向到主頁對您的SEO不利。

編輯:我只是意識到你是通過URL到「ID」。所以我刪除了關於使用.而不是^(.*)$的第二條評論。仍然錯誤頁面是你最好的選擇。您可以使用$_SERVER['REQUEST_URI'](如果避免硬重定向)來獲取URL。

page有一些示例配置,可能對您有所幫助。它有幾個WordPress配置。

+0

爲你鏈接的是'ifIsEvil'頁解釋說,一個有回報或重寫是沒有問題的,所以這不是問題(儘管try_files通常是更好的方法) – cobaco