2014-10-28 56 views
1

我想改写这个URL(是的,URL有一个问号):domain.com/blog/blogdetail?id=20Nginx的重写与问号

这样:domain.com/train/my-page

这是我现在,但它不工作:

location /blog { 
    rewrite ^/blog/blogdetail(.*)id=20$ $scheme://$host/train/my-page permanent; 
} 

我不确定是怎么回事。我尝试过location /blog/并在正则表达式中跳过/。

任何想法我失踪或另一种方式去做这件事?基本上,我有一堆具有特定ID#的页面,我需要重写它们各自的“基于字的”URL。

+0

告诉你什么是你的预期产出。 – depsai 2014-10-29 04:51:12

+0

Nginx仅重写uri的“路径”部分而无需查询。 – 2014-10-29 10:49:14

+0

@depsai我的预期产出是问题的第一部分。 – shlant 2014-10-29 15:46:33

回答

2

你在这里有错误的逻辑,它应该是一个干净的URL重写到一些花哨的内部,而不是相反。你们之间也应该有一个干净的关系。

无论如何,nginx在大部分指令中都不匹配参数,包括位置和重写。更简单的方法是通过map指令。

map $request_uri $target_uri { 
    "~/blog/blogdetail\?id=20$" "/train/my-page"; 
} 

server { 

    ... 

    location /blog { 
     rewrite ^/blog/blogdetail$ $scheme://$host$target_uri? permanent; 
    } 

}