【问题标题】:Rewrite url segments to get parameters nginx重写url段获取参数nginx
【发布时间】:2016-11-17 15:39:19
【问题描述】:

我现在正在尝试几个小时来解决这个问题,这应该不是那么令人头疼的问题。

我需要正确的 nginx 服务器代码来完成此操作。

我有一个这样的网址: https://www.example.com/share/userid/file/

我想将其重写为: https://www.example.com/share/?id=userid&key=file/

我已经在 stackoverflow 中尝试了几种解决方案,但都没有奏效。

我试过了:

rewrite ^/share/(.*)/(.*)$ /share/index.php?id=$1&key=$2 ;

如果我在那里放置一个 index.php 文件,那会起作用,但是我不能这样做,因为 /share/ 是一个永久链接,而不是一个实际的文件夹(wordpress)。如果我这样做:

rewrite ^/share/(.*)/(.*)$ /share/?id=$1&key=$2 ;

我收到 404 nginx 错误。

似乎 ^/share/(.)/(.)$ 实际上并没有触发重写,所以这一定是错误的,尽管我正在查看 nginx rewrite 文档并看起来好的。

有什么想法吗?我也尝试了 try_files,但没有成功。

谢谢

更新:

好的,这样就可以了

rewrite ^(/share/)([0-9]+)/(.*)/$ https://$server_name/share/?id=$2&key=$3 last;

但是由于某种原因,如果我这样做它不起作用:

rewrite ^(/share/)([0-9]+)/(.*)/$ /share/?id=$2&key=$3 last;

我必须添加 $server_name 才能使其工作,谁能解释一下为什么?

【问题讨论】:

    标签: url nginx parameters url-rewriting get


    【解决方案1】:

    我终于自己解决了这个问题:

    rewrite ^(/share/)([0-9]+)/(.*)/$ https://$server_name/share/?id=$2&key=$3 last;
    

    我使用([0-9]+) 因为第一个参数是整数

    但是由于某种原因,如果我这样做它不起作用:

    rewrite ^(/share/)([0-9]+)/(.*)/$ /share/?id=$2&key=$3 last;
    

    我必须添加

    https://$server_name
    

    问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-29
      • 1970-01-01
      • 2016-06-21
      • 2015-01-10
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多