【问题标题】:How to rewrite url in php using htaccess file如何使用 htaccess 文件重写 php 中的 url
【发布时间】:2014-04-30 01:12:02
【问题描述】:

在我的项目中添加 .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 是

主机名/站点1/类别

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

主机名/site1/categories/page/0

【问题讨论】:

  • RewriteRule ^/categories/?$ category.php [L] &lt;!-- LINEBREAK HERE --&gt; RewriteRule ^/categories/page/([^/]+)/? category.php?page=$1 [L]?
  • @Scott 感谢重播,你是真的,但我已经得到解决方案

标签: php .htaccess url url-rewrite-module


【解决方案1】:
  1. 我认为问题是当您第一次点击类别按钮时,href 属性的值为“hostname/site1/categories/page/0”,请在您第一次加载此页面时检查 $page 的值。
  2. 您可以删除重复的重写规则。

【讨论】:

  • 第一次 $page 值默认为 0,然后当我单击下一步按钮时它将递增到 $page=$page+1。
【解决方案2】:

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

如果您有相同的 quote.php 页面和您的网址,例如

site1/testing/quote/category/

site1/testing/quote/category/love-quotes

site1/testing/quote/category/love-quotes/page/1

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

RewriteRule quote/category/(.)/page/(.)/quote.php?category=$1&page=$2 [NS]

RewriteRule quote/category/(.)/page/(.) quote.php?category=$1&page=$2 [NS]

重写规则引用/类别/(.*)/$ quote.php?category=$1 [L]

重写规则引用/类别/(.*)$ quote.php?category=$1 [L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多