Pagination – php .htaccess paging code for seo

Over the last 2 weeks i’ve been working a lot on paging in php.

so… Pretty simple to get first last next and previous to work in php.

Looking at resources on the web I haven’t seen many paging php example including rewriting url for the search enignes so thought i’d put some code up here on my blog.. Hope it helps!

mysql_select_db($database, $con);

$query_prods = $query_list= “SELECT * FROM table”;
//echo $query_prods ;

$query_limit_prods = sprintf(“%s LIMIT %d, %d”, $query_prods, $startRow_prods, $maxRows_prods);
$prods = mysql_query($query_limit_prods, $con) or die(mysql_error());
$row_prods = mysql_fetch_assoc($prods);

The above code is a simple example of the php to create a recordset for the entire contents of “table”..

$maxRows_prods = 6;
$pageNum_prods = 0;

$all_prods = mysql_query($query_prods);
$totalRows_prods = mysql_num_rows($all_prods);
$totalPages_prods = ceil($totalRows_prods/$maxRows_prods)-1;

$queryString_prods = “”;
 $params = explode(“&”, $HTTP_SERVER_VARS[‘QUERY_STRING’]);
 $newParams = array();
 foreach ($params as $param) {
   if (stristr($param, “pageNum_prods”) == false &&
       stristr($param, “totalRows_prods”) == false) {
     array_push($newParams, $param);
 if (count($newParams) != 0) {
   $queryString_prods = “&” . implode(“&”, $newParams);

The above part of the php paging code creates variables to create a simple mod-rewrite php paging.


$currentPage = “some-keyword/”

if ($pageNum_prods > 0) { // Show if not first page ?>
           <a href=”<?php echo $currentPage; ?>”><img src=”first.gif” border=”0″></a>
           <?php } // Show if not first page ?> </p>
       <?php if ($pageNum_prods > 1) { // Show if not first page ?>
           <a href=”<?php echo $currentPage.”/”.($pageNum_prods – 1); ?>”><img src=”prev.gif” border=”0″ align=”bottom”></a>
           <?php } // Show if not first page ?>
     <?php if ($pageNum_prods < $totalPages_prods) { // Show if not last page ?>
           <a href=”<?php echo $currentPage.”/”.($pageNum_prods + 1); ?>”><img src=”next.gif” border=”0″></a>
           <?php } // Show if not last page ?>
   <?php if ($pageNum_prods < $totalPages_prods) {?><a href=”<?php echo $currentPage.”/”.($totalPages_prods); ?>”><img src=”last.gif” border=”0″></a><?php }?>

The above code shows image for first, next, previous and last however you can of course slam in some keywords here either in the alt tags or as text.. I’ll explain how in a bit..

RewriteCond %{REQUEST_URI} ^/somekeyword/([0-9]+)
RewriteRule ^(.*) /page.php?pageNum_prods=%1Â [L]

The above is the .htacess for paging a php page called page.php in the root of the site. Please note “somekeyword” can be changed to theme the paging for example if the query returned all products that are “red” then the folder could be called “red” to give a little seo help..

The above coding will result in for page 1 for page 2 and so on..

The above is a very simple basic script which I hope helps however now you can start to take it up a gear in regards to seo..

So paging has always been a issuse with SE as page 1 will return the same title des and k/w as page 2 and so on … so how to fix the dup content?

Well always consider the user so look at the result on the page.. Look at them as a human.. What is on the page? How could you describe it ? The above php limits to 6 results per page.. those 6 items each have a title.. Now depenant on your data the is the key!..

Lets assume your in the RED section of the site so the query retrun all red items.. In the first 6 you have what type of items ? These items are the content of the page along with what that page links to.. By extract the type of items your be able to add them to title and desriptions..

So again assuming RED items include pens,ink,paper,paint… collect this data in array.. Now the key so make sure its reads well to the user. So something like “page 1 red items” could be change to “Red pens, ink paper and paint from xyz”

Basically your giving the user the title + description to what is on the page.. as above if page 2 contained card, cloth,fabric,thick paper .. The title would change from something like “page 2 red items” to “Red card, cloth, fabric and thick paper from xyz”

Of course using “,” in titles and description isn’t a great idea thus its worth looking into this wording dependant on your niche..

Hope that helps someone a little 🙂

This entry was posted in Misc. Bookmark the permalink.