用 nginx 重写规则用 slach 替换 get 变量 [英] Rewrite rule with nginx replace get variable with slach
问题描述
这两天我一直在阅读我能找到的所有内容,但似乎没有任何效果
I've been reading everything i could find for two days now but nothing seems to work
我有一个这样的网址
http://www.mysite.com/auction.php?category=23140
并希望制定重写规则,使其看起来像那样
and want make a rewrite rule so it will appears like that
http://www.mysite.com/auction/category/23140.php
我这样做只是为了构建 seo 友好的 url,但到目前为止没有任何效果.
I do that only in order to build seo friendly url but so far nothing have worked.
最后重写^/auction.php?category=(.*)$/auction/category/$1;
rewrite ^/auction.php?category=(.*)$ /auction/category/$1 last;
看起来很简单,我真的不知道为什么它不起作用.
It look so simple i realy don't know why it doesn't work.
我应该将此行放在服务器块还是位置块中?正则表达式错了吗?
Should i put this line in the server block or the location block ? Is the regex wrong ?
任何帮助将不胜感激
推荐答案
首先,请记住 rewrite 在 nginx 不匹配查询字符串,只有 uri.
At first, pls remember rewrite in nginx does not match query string, only uri.
例如,在这个 url: "/auction.php?category=23140" 中,uri 是 /auction.php 并且查询字符串是 category=23140
For example, in this url: "/auction.php?category=23140", uri is /auction.php and query string is category=23140
所以在你的情况下,正确的规则应该是:
So in your case, the correct rule should be:
if ( $request_uri ~ "^/auction\.php\?category=[0-9]+(.*)?$" ) {
rewrite "^.*$" /auction/category/$arg_category break;
}
以上只是一个例子,您可以根据您的情况进行更改.您还可以找到 $request_uri 和 $arg_PARAMETER 来自 nginx 手册.
Above is just a example, you can change it according to your case. And also you can find the usage of $request_uri and $arg_PARAMETER from the nginx manual.
这篇关于用 nginx 重写规则用 slach 替换 get 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!