Nginx URL Rewrite with Multiple Parameters

2019-07-18 10:34发布

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条回答
Bombasti
2楼-- · 2019-07-18 11:25

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

查看更多
登录 后发表回答