简体   繁体   中英

How to rewrite url in php using htaccess file

pagination is not working after adding .htaccess file on my project.

Code for Paging is :

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

when i click on categories link it display 100 categories and url is

hostname/site1/categories

but when i click on next link it will display same content of 100 categories and url is

hostname/site1/categories/page/0

  1. I believe problem is when your click category button first time,the href attribute's value is "hostname/site1/categories/page/0",check $page's value when your first time load this page.
  2. you could delete your duplicate rewrite rule.

I got answer of my question and i am posting here because help to other developer to have same question.

If you have same quote.php page and your urls like

site1/testing/quote/category/

site1/testing/quote/category/love-quotes

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

and add those rules on your .htaccess file.

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

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

RewriteRule quote/category/(.*)/$ quote.php?category=$1 [L]

RewriteRule quote/category/(.*)$ quote.php?category=$1 [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM