2013-03-10 128 views
18

我想在我的nginx服務器中使用重寫功能。nginx重寫無需更改url

當我嘗試「http://www.example.com/1234」時,我想重寫「http://www.example.com/v.php?id=1234」,並想在瀏覽器中獲得「http://www.example.com/1234」。

這裏是nginx.conf文件

... 
    location ~ /[0-9]+ { 
     rewrite "/([0-9]+)" http://www.example.com/v.php?id=$1 break; 
    } 
... 

當我嘗試 「http://www.example.com/1234

我想...

url bar in browser : http://www.example.com/1234 
real url : http://www.example.com/v.php?id=1234 

,但我有麻煩......

url bar in browser : http://www.example.com/v.php?id=1234 
real url : http://www.example.com/v.php?id=1234 

回答

28

參考:http://wiki.nginx.org/HttpRewriteModule#rewrite

如果替換字符串以http://開頭,則客戶端將被重定向,並且任何其他>重寫指令都將終止。

所以刪除HTTP://部分,它應該工作:

location ~ /[0-9]+ { 
     rewrite "/([0-9]+)" /v.php?id=$1 break; 
}