【问题标题】:nginx rewrite rule not working?nginx重写规则不起作用?
【发布时间】:2015-07-11 02:58:39
【问题描述】:
rewrite ^/index\.asp /index.php last;
rewrite ^/index\.asp\?boardid=([0-9]+)$ /forum-$1-1.html last;
rewrite ^/index\.asp\?boardid=([0-9]+)(.*)$ /forum-$1-1.html last;
rewrite ^/index_([0-9]+)(.*)$ /forum-$1-1.html last;
rewrite ^/dispbbs\.asp\?boardID=([0-9]+)&ID=([0-9]+)$ /thread-$2-1-1.html last;

我已经尝试了上面的重写规则,并得到一个死的结果,不工作。 我参考了很多帖子和文章,但没有任何帮助。

有什么错误吗?

V/R, 加文


感谢您的回复。 :)

我已将我的 nginx 配置更改为,

rewrite ^/index\.asp$ /index.php last;
rewrite ^/index\.asp\?boardid=([0-9]+)(.*)$ /forum-$1-1.html last;
rewrite ^/index\.asp\?boardid=([0-9]+)$ /forum-$1-1.html last;
rewrite ^/dispbbs\.asp\?boardID=([0-9]+)&ID=([0-9]+)$ /thread-$2-1-1.html last;

还是不行。但我发现规则没有错误。

【问题讨论】:

    标签: nginx rewrite


    【解决方案1】:

    您不能匹配 rewrite 规则中的参数,它们可能只包含路径。原因很简单:假设参数可能有另一个顺序;假设您可能没有考虑其他参数(例如来自 Google 的关键字)。

    所以你的规则应该被重写以首先匹配路径然后检查参数。像这样:

    rewrite ^/index_([0-9]+)(.*)$ /forum-$1-1.html last;
    
    location /index.asp {
      if ($arg_boardid ~ "^([0-9]+)") {
        rewrite ^ /forum-$1-1.html break;
      }
      rewrite ^ /index.php break;
    }
    
    location /dispbbs.asp {
      rewrite ^ /thread-$arg_ID-1-1.html break;
    }
    

    【讨论】:

    • 谢谢。但是,我也有像 rewrite ^/dispbbs_([0-9]+)_([0-9]+)\.html$ /thread-$2-1-1.html last;这样的规则> 在 nginx.config 中,它们运行良好。我会试试你的seggestions。 :)
    • 重写 ^/dispbbs_([0-9]+)_([0-9]+)\.html$ /thread-$2-1-1.html 最后;代码>
    • 参数(查询字符串)在问题字符之后。你可以匹配之前的任何东西,因为它是一个字符串,但是你不能对查询字符串进行匹配。
    • 应该读作“因为它是一个路径”而不是“因为它是一个字符串”。
    • 感谢您的耐心。 :) rewrite ^/dv_rss\.asp(.*) /archiver/ last; 但是,这个很完美。
    猜你喜欢
    • 2013-02-24
    • 1970-01-01
    • 2012-04-24
    • 2014-05-16
    • 2013-07-06
    • 1970-01-01
    相关资源
    最近更新 更多