【问题标题】:NGINX Rewrite Rules For PHP PaginationPHP分页的NGINX重写规则
【发布时间】:2019-02-12 11:51:05
【问题描述】:

我当前的分页网址如下:

http://localhost:8000/index.php?slug=the-category-name&type=category&page=1

我想要的网址应该是:

https://mywebsite.com/category/the-category-name/page/1/

如果我只想查看没有分页的类别,我会使用此重写规则:

location /category/ {
            rewrite ^(.*[^/])$ $1/ permanent;
            rewrite  ^/category/(.*)/$ /index.php?slug=$1&type=category 
            last;
    }

这就是我获取 URL 的方式:

https://mywebsite.com/category/the-category-name/

为了能够创建分页,我需要在此配置中进行哪些更改(附加到):

【问题讨论】:

    标签: nginx pagination


    【解决方案1】:

    rewrite 语句按顺序计算,并在包含 flag 的语句与 URI 匹配时停止。详情请见this document。

    您应该将更具体的正则表达式放在不太具体的表达式之前。

    例如:

    location /category/ {
        rewrite ^(.*[^/])$ $1/ permanent;
        rewrite  ^/category/(.*)/page/(.*)/$ /index.php?slug=$1&type=category&page=$2 last;
        rewrite  ^/category/(.*)/$ /index.php?slug=$1&type=category last;
    }
    

    为了使正则表达式更加具体,您可以使用([^/]+) 来捕获slug 并使用(\d+) 来捕获页码。

    【讨论】:

    • 我一直在尝试完全相同的字符串,但它对我不起作用。但是,我刚刚复制并粘贴了您的,它可以工作。现在我正试图找出不同之处。谢谢!
    • 你能告诉我如何摆脱 /category/page/0/ 吗?我需要使用任何重写规则,还是在 PHP 端更好?
    • 应该如何回复/category/page/0/?目前,它将匹配您的正则表达式并创建一个slug=page/0。如果您想避免这种情况,请按照我的回答中的建议更改捕获。
    猜你喜欢
    • 1970-01-01
    • 2016-06-20
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    相关资源
    最近更新 更多