php - How to remove page from pagination URL -


i have written seo-friendly pagination function working fine. however, current url looks this:

1. localhost/test/tastepage.php/test/testpage/page/123

but want accomplish is:

2. localhost/test/testpage.php/page/123 or localhost/test/testpage.php/123

when go url bar , edit url 2 working fine. want know, why url 1 whenever load page first time?

here have in tastpage.php file:

$page=1;//set page number 1 default $sitepath=explode('/',parse_url($_server['request_uri'],php_url_path));//removing slashes url , slicing url array if(is_array($sitepath)&& !empty($sitepath)){     if($keys=array_search('page',$sitepath)){;     print_r($keys);     $page=(int)$sitepath[$keys+1];     } } function paginate($page,$display,$total){ $sitepath=parse_url($_server['request_uri'],php_url_path);   if($sitepath!='/'){       $sitepath=rtrim($sitepath,'/');       if(stristr($sitepath,'page/')){           $sitepath=rtrim($sitepath,'0..9');           $query='';       }       else {          $query=$sitepath .'/page/';      }   }       else {         $query = 'page/';     } $pages=ceil($total/$display );  $first='<a href="'.$sitepath.$query.'1">first</a>'; $last='<a href="'.$sitepath.$query.$pages.'">last</a>'; $next='<li><a href="'.$sitepath. $query . ($page + 1) . '">»</a></li>'; $preview='<li><a href="'.$sitepath.$query.($page-1).'">«</a></li>'; $numbers=""; ($i=max(1,$page-5);$i<=min($page+1,$pages);$i++) {     if($i==$page){     $numbers.='<li><a class="active" href="'.$sitepath.$query.$i.'">'.$i.'</a></li>';     }     else {         $numbers.='<li><a href="'.$sitepath.$query.$i.'">'.$i.'</a></span></li>';     } }    if($page>1){      echo $preview;    }   echo  $numbers;      if($page<$pages){       echo  $next;    }     };  $limit = 10; //count how many rows  $total = $conn->query("select count(*) pagi")->fetchcolumn();  if ($total > 0) {     $start = $limit * $page - $limit;     $sql = $conn->prepare("select * pagi order pg_id limit :start, :limit");     $sql->bindvalue(':start', $start, pdo::param_int);     $sql->bindvalue(':limit', $limit, pdo::param_int);     $sql->execute();     if ($sql->rowcount() > 0) {         while ($result = $sql->fetch(pdo::fetch_assoc)) {             echo $result['pg_name'],"<br />";         }     } } else {     echo "no result fetch"; }  ?> 

my answer alternative solution url problem. please put '.htaccess' file on root of website. place code on .htaccess:

rewriteengine on  rewriterule ^/?testpage/(.*) testpage.php?page=$1 [l] 

then can current page number php code on testpage.php:

<?php $page = (isset($_get['page']) ? (int)$_get['page'] : 1);//holds current page echo $page; ?> 

try these urls browser test if solution works:

for explanation try reading article: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo