如何在php中使用htaccess文件重写url


How to rewrite url in php using htaccess file

在我的项目中添加。htaccess文件后分页不工作。

分页代码为:

if ($page > 0) {
            $last = $page - 2;
            echo "<a href=" . $this->SITE_URL . "categories/page/$last>Previous</a>";
            echo "<a href=" . $this->SITE_URL . "categories/page/$page>Next</a>";
        } else if ($page == 0) {
            echo "<a href=" . $this->SITE_URL . "categories/page/$page>Next</a>";
        } else if ($left_rec < $rec_limit) {
            $last = $page - 2;
            echo "<a href=" . $this->SITE_URL . "categories/page/$last>Previous</a>";
        }
.htaccess file have
RewriteRule categories category.php
RewriteRule categories/page/(.*)/ category.php?page=$1
RewriteRule categories/page/(.*) category.php?page=$1

当我点击类别链接时,它显示100个类别,url是

主机名/site1/类别

但是当我点击下一个链接它将显示100个类别的相同内容,url为

主机名/site1/类别/页面/0

  1. 我相信问题是当你第一次点击类别按钮,href属性的值是"hostname/site1/categories/page/0",检查$page的值当你第一次加载这个页面。
  2. 你可以删除重复的重写规则。

我得到了我的问题的答案,我在这里发布是因为帮助其他开发人员有同样的问题。

如果你有相同的quote.php页面和url,如

site1/测试/报价/类别/

site1/测试/报价/类别/喜欢

site1/测试/报价/类别/喜欢//1页

并将这些规则添加到你的。htaccess文件中。

RewriteRule报价/类别/(. )/页面/(。)/quote ?类别= 1美元,页面= 2美元(NS)

RewriteRule报价/类别/(. )/页面/(. ) quote ?类别= 1美元,页面= 2美元(NS)

rewriterrule quote/category/(.*)/$ quote.php?类别= 1美元[L]

rewriterrule quote/category/(.*)$ quote.php?类别= 1美元[L]