nginx 带query string argument 的URL 跳转

nginx 自带的三个命令,  return, rewrite, try-files功能非常强大,基本可以完成各式各样的跳转, 比如下面的:

server {

    listen 80;

   server_name example.com;

   return 301 https://www.example.com$uri;

}

这样就完成了一个不带www的domain转向带SSL以及www前缀的domain的跳转

$uri: 按照nginx官方文档的话来说,就是current normalized URI in REQUEST

$request_uri: full original request URI (with arguments)

这里很明显就看到了$uri和$request_uri的区别,$uri 是 normalized的,换句话说,就是

  1. 去除了?以及后面的query参数
  2. encoded URL被 decoded

因此这里就出现了一个问题: exapmle.com/test.php?a=b 会跳转到https://www.example.com/test.php, query string丢失了,解决的办法很简单

return 301 https://www.example.com$uri$is_args$args

$is_args is an emprt string if there are no arguments, or a ? to signify the start of the query string.

$args then adds the arguments,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.