如何编写这个nginx重写规则

时间:2015-12-09 07:52:32

标签: nginx url-rewriting nginx-location

我有像这样的nginx重写规则

        location ~* /question\-(.*)\.html$ {

                rewrite  "^/question-([0-9]+).html$" "/question/$1.html";
                rewrite  "^/question-([0-9]+).html$"  /question.php?id=$1&lm=&pn= break;

         }

这条规则意味着: 如果URI是/question-123456.html那么重写为/question/123456.html /question/123456.html是静态文件,并通过question.php来创建。

所以当我访问http://example.com/question-123456.html HTTP重写到http://example.com/question/123456.html时如果不存在,我想执行下一次重写rewrite "^/question-([0-9]+).html$" /question.php?id=$1&lm=&pn= break;

除了向用户返回404之外。

1 个答案:

答案 0 :(得分:0)

我会这样写:

location ~ /question-(.*)\.html$ {
    try_files /question/$1.html /question.php?id=$1;
}