使用多个参数重写Nginx URL [英] Nginx URL Rewrite with Multiple Parameters
问题描述
我正在尝试通过Nginx重写以下URL:
I am trying to rewrite the following URL via Nginx:
http://www.domain.com/script.php?title=LONGSTRING&desc=LONGSTRING&file=LONGSTRING&id=THREELETTERS
变成这样:
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 "/".
推荐答案
您可以通过使用脚本参数将网址重写为对seo友好的网址,然后变为简单的重写,如下所示:
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;
}
相反,将seo友好的URL重写为php脚本版本将是:
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;
}
基本上,您有正则表达式捕获(每个圆括号对都是捕获),然后可以使用$ 1,$ 2,...变量进行引用
Basically you have regex captures (each round bracket pair is a capture) that you can then reference with the $1, $2, ... variables
这篇关于使用多个参数重写Nginx URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!