Nginx URL Rewrite with Multiple Parameters

梦想的初衷 提交于 2019-12-13 16:09:26

问题


I am trying to rewrite the following URL via Nginx:

http://www.domain.com/script.php?title=LONGSTRING&desc=LONGSTRING&file=LONGSTRING&id=THREELETTERS

into something like this:

http://www.domain.com/script/LONGSTRING/LONGSTRING/LONGSTRING/LONGSTRING/THREELETTERS.html

All I have been able to find so far is how to include a single variable. I have five variables to pass through, each terminated by a "/".


回答1:


you can access a script parameter name in nginx through the $arg_name variable

rewriting the url with the script parameters to an seo-friendly url then becomes a simple rewrite like so:

location /script/script.php {
  rewrite ^ /script/$arg_title/$arg_desc/$arg_file/$arg_id.html last;
}

the reverse, rewriting the seo-friendly url to the php script version would be:

location /script/ {
  rewrite "^/script/([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z]{3})$" 
          /script.php?title=$1&desc=$2&file=$3&id=$4 last;
}

Basically you have regex captures (each round bracket pair is a capture) that you can then reference with the $1, $2, ... variables



来源:https://stackoverflow.com/questions/13016427/nginx-url-rewrite-with-multiple-parameters

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