Url rewriting Nginx简单重写

Url rewriting Nginx简单重写,url-rewriting,nginx,Url Rewriting,Nginx,我想在nginx中创建以下url comments.php?id=34 变成 /comments/34 /comments/34/ 我正在尝试这个,它的工作 rewrite ^/comments/$id/(.*)$ /comments.php?id=$1? last; 我的问题是,如何强制将comments.php?id=x重定向到/comments/id根据,重写只在路径上运行,不在参数上运行 请尝试以下方法: if ($args ~ id=(.+)){ rewrite com

我想在nginx中创建以下url

comments.php?id=34
变成

/comments/34
/comments/34/
我正在尝试这个,它的工作

rewrite  ^/comments/$id/(.*)$  /comments.php?id=$1?  last;
我的问题是,如何强制将comments.php?id=x重定向到/comments/id

根据,重写只在路径上运行,不在参数上运行

请尝试以下方法:

if ($args ~ id=(.+)){
  rewrite comments\.php /comments/$1 last;
}

你能解释一下吗。你的意思是在我重写的同时加上这个。那只是一个建议;正如最初发布的一样,它将使用?id=X参数将任何请求重写为/comments/X。用comments.php替换“^”应该可以得到您想要的,我将编辑以显示这一点。您需要弄清楚这与您的/comments/$id/$userid请求的匹配程度-重写顺序很重要,但在我看来,您的URL模式不明确。换句话说,rewrite指令只看到请求中位于?之前的部分?。您需要使用if块来测试参数。将^with comments替换为comments是什么意思。php文档中最初的rewrite命令(链接为rewrite^/comments/$1 last)重写了与if语句匹配的所有URL。因此,我用comments\.php替换了该字符串中的“^”,因此它只会重写包含comments.php的URL。将其更改为答案,因为这是一个更为最新、总体更好的解决方案。
rewrite ^/comments.php$ /comments/$arg_id? permanent;