用于PHP分页的NGINX重写规则

时间:2019-02-12 11:51:06

标签: nginx pagination

我当前的分页网址如下:

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

我想要的URL应该是:

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

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

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

这是我获取网址的方式:

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

我需要在此配置中更改(附加到)什么以便能够创建分页,例如:

1 个答案:

答案 0 :(得分:1)

v-if语句按顺序求值,当包含 flag 的语句与URI匹配时停止。有关详细信息,请参见this document

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

例如:

rewrite

要使正则表达式更具体,可以使用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; } 来捕获段代码,并使用([^/]+)来捕获页码。