【问题标题】:Nginx Rewrite Rules for rewriting html files to php not working将html文件重写为php的Nginx重写规则不起作用
【发布时间】:2014-01-08 19:58:46
【问题描述】:

我正在尝试将所有 HTML 文件重写为 nginx 中的 PHP 文件。所以我在我的 nginx conf 文件中输入了以下规则。

location ~ \.html$
{      rewrite (.*).html$ /$1.php;
}

但它不起作用,所以我尝试将重写规则放在belwo位置,但即使这样也不起作用。

location / {
  rewrite (.*).html$ /$1.php;
  rewrite admin/$ /admin/index.php;
  rewrite ([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /productlist.php?cat=$1&subcat=$2;
  rewrite ([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /productlist.php?cat=$1&subcat=$2&brand=$3;
  rewrite ([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([0-9]+)/buy/$ /productdetails.php?cat=$1&subcat=$2&product=$3&id=$4;
  rewrite ([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([0-9]+)/buy$ /productdetails.php?cat=$1&subcat=$2&product=$3&id=$4;
  rewrite admin/internalattacment/(.*) /download.php/$1;
  rewrite admin/report_bug/(.*) /download.php/$1;
}

但是当我这样设置重写规则时,它被 nginx 拾取

location ~ .*.()$ {
  rewrite (.*).html$ /$1.php;
}

有人可以告诉我为什么没有从前 2 个位置获取重写规则吗?

【问题讨论】:

    标签: php .htaccess http nginx rewrite


    【解决方案1】:

    我认为你的错误在于位置。

    location ~ \.html$
    

    只匹配“.html”,不匹配“anything.html” 试试

    location ~ /(.*).html$
    

    【讨论】:

    • 不......它没有用。 php 的相同规则,即 "location ~ \.php$" 工作正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2016-06-20
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 2016-08-09
    相关资源
    最近更新 更多