Rewrite rule with nginx replace get variable with slach

陌路散爱 提交于 2021-02-08 05:55:09

问题


I've been reading everything i could find for two days now but nothing seems to work

I have an url like this

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

I do that only in order to build seo friendly url but so far nothing have worked.

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 ?

Any help will be greatly appreciated


回答1:


At first, pls remember rewrite in nginx does not match query string, only uri.

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; 
}

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.




回答2:


You're doing it backwards, try this.

rewrite ^/auction/([^/]*)/([0-9]*) /auction.php?$1=$2

piece of advice, if this is a custom application you're writing without a framework, you could find a routing module that you could use and it would help you do every thing easily.



来源:https://stackoverflow.com/questions/17721651/rewrite-rule-with-nginx-replace-get-variable-with-slach

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!