Nginx 重写有和没有尾部斜杠
Nginx rewrite with and without trailing slash
我正在尝试创建一组匹配 url 的规则,带和不带尾部斜杠
大多数答案都指向我使用与此类似的东西。
location /node/file/ {
rewrite ^/node/file/(.*)/(.*)$ /php/node-file.php?file=&name=;
rewrite ^/node/file/(.*)/(.*)/?$ /php/node-file.php?file=&name=; │
}
但这与结尾的斜杠不匹配 url。
我如何编写匹配 url 看起来像
的规则
http://example.com/node/file/abcd/1234/
http://example.com/node/file/abcd/1234
第一个 rewrite
语句包括 (.*)
作为最后一个捕获,它将匹配任何字符串,包括带有尾部斜杠的字符串。
使用字符 class [^/]
匹配除 /
:
之外的任何字符
rewrite ^/node/file/([^/]*)/([^/]*)$ /php/node-file.php?file=&name=;
rewrite ^/node/file/([^/]*)/([^/]*)/?$ /php/node-file.php?file=&name=;
现在您会注意到第一个 rewrite
语句是不必要的,因为第二个 rewrite
语句匹配带有和不带有尾随 /
.
的 URI
所以你只需要:
rewrite ^/node/file/([^/]*)/([^/]*)/?$ /php/node-file.php?file=&name=;
我正在尝试创建一组匹配 url 的规则,带和不带尾部斜杠
大多数答案都指向我使用与此类似的东西。
location /node/file/ {
rewrite ^/node/file/(.*)/(.*)$ /php/node-file.php?file=&name=;
rewrite ^/node/file/(.*)/(.*)/?$ /php/node-file.php?file=&name=; │
}
但这与结尾的斜杠不匹配 url。
我如何编写匹配 url 看起来像
的规则http://example.com/node/file/abcd/1234/
http://example.com/node/file/abcd/1234
第一个 rewrite
语句包括 (.*)
作为最后一个捕获,它将匹配任何字符串,包括带有尾部斜杠的字符串。
使用字符 class [^/]
匹配除 /
:
rewrite ^/node/file/([^/]*)/([^/]*)$ /php/node-file.php?file=&name=;
rewrite ^/node/file/([^/]*)/([^/]*)/?$ /php/node-file.php?file=&name=;
现在您会注意到第一个 rewrite
语句是不必要的,因为第二个 rewrite
语句匹配带有和不带有尾随 /
.
所以你只需要:
rewrite ^/node/file/([^/]*)/([^/]*)/?$ /php/node-file.php?file=&name=;