nginx 301redirect查询string

我正试图redirect这个丑陋的url,

/index.php/component/qs/?com=qs&id=1234 

至,

 /product/?id=1234 

所以我想这样做,

 server { listen 443 ssl http2 default_server; listen 443 [::]:443 ssl http2 default_server; server_name www.example.com; root /home/example/public_html/; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; if($query_string ~ "id=(\d+)") { rewrite ^.*$ /products/?id=$1 permanent; }} location ~ \.php$ { include snippets/fastcgi-php.conf include /etc/nginx/fastcgi_params; fastcgi_pass unix:/run/php/php7.0-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name; fastcgi_intercept_errors on; } } 

当我运行nginx -t我得到以下错误,

 nginx: [emerg] unknown directive "if($query_string" 

我有这个在Apache下工作,但对Nginx来说是新的,任何帮助将不胜感激。

nginx将URL查询参数存储在$arg_name参数中。

因此,你可以在你的if语句中使用$arg_id 。 此外,您应该在您的location /指令之前使用另一个位置:

 location /index.php/component/qs { if ($arg_id) { rewrite ^ /products/?id=$arg_id permanent; } } 

如果$arg_id是一个空string,则不会执行if语句。 在rewrite^是告诉它重写任何URL的最简单forms。 由于URL和id参数较早匹配,因此在rewrite语句中不需要进行任何匹配。

if指令名称后面有空格。 它应该是:

 if ($query_string ~ "id=(\d+)") 

 if($query_string ~ "id=(\d+)")