2015-12-18 266 views
1

我怎樣才能解決這個問題:我想成立Nginx的的c​​onf文件,以滿足以下條件:nginx的重寫URL子

http://www.example.com/site1/article/index.php?q=hello-world - >http://www.example.com/site1/article/hello-world

HTTB://www.example.com/site2








.php?q = open-new-world - > httb://www.example.com/site3/article/open-new-world

還有多在example.com之後的網站,我想通過使用nginx配置使網址看起來乾淨。

但我的下面的配置不起作用。誰來幫幫我?

server { 
listen 80; 
listen [::]:80; 

root /var/www/example.com/public_html; 
index index.php index.html index.htm; 

server_name www.example.com;  
location ~ /article/ { 
    try_files $uri /site1/article/index.php?q=$1; 

    location ~ \.php$ { 
      try_files $uri =404; 
      fastcgi_split_path_info ^(.+\.php)(/.+)$; 
      fastcgi_pass unix:/var/run/php5-fpm.sock; 
      fastcgi_index index.php; 
      include fastcgi_params; 
    } 
} 

}

回答

0

你想在客戶端提供URL像/xxx/article/yyy,然後在內部改寫爲/xxx/article/index.php?q=yyy

您需要捕獲源URI的組件以便稍後使用它們。您的問題中有一個$1,但您錯過了實際爲其賦值的表達式。隨着變化的最小數量,這個工程:

location ~ ^(.*/article/)(.*)$ { 
    try_files $uri $1index.php?q=$2; 
    location ~ \.php$ { ... } 
} 

但是,您不需要使用PHP嵌套位置,只要出現在PHP正則表達式的位置上述其他正則表達式的位置,它會處理所有的PHP文件。例如:

location ~ \.php$ { ... } 

location ~ ^(.*/article/)(.*)$ { 
    try_files $uri $1index.php?q=$2; 
} 
+0

現在工作!謝謝@理查德史密斯 – Jonash