Case Case - 1 month ago 8
PHP Question

Re write rule refuses to function pagination

I am trying to get my url for my pagination to display as follows

example.com/members/p1

as of now it shows

example.com/members.php?page=1

I have tried the re write below. don't from fr write generator but it does not work

.htaccess
:

RewriteEngine on
RewriteRule ^p/([^/]*)$ members.php?page=$1 [L]


PHP:

<?php
$number_list = array();
for ($i = 1; $i <= $count; $i ++) {

if ($i == $page) {

echo "<li><a class='active' href='members.php?page={$i}'>{$i}</a></li>";

} else {

echo "<li><a href='members.php?page={$i}'>{$i}</a></li>";
}
}
?>

Answer

You need to separate between the code you output to the user (using PHP) and mod_rewrite (which translate the pretty-URLs you have to the actual URLs with the variables your PHP code handles).

Lets say you want to write a link to example.com/members/p1. Your php code should look like this:

$i = 1;
echo "<li><a href='members/p{$i}'>{$i}</a></li>";

Now - when someone enter the page members/p1 - you want your PHP code to think that this someone actually entered users.php?page=1 (it's much easier because this way you have the variable $_GET['page'] you can use.

So, to sum it up: 1. In your PHP code - you generate "pretty" links. 2. In your .htaccess file you tell the server to "think" that when it gets a request to ^members/([^/][0-9]*)$ it should treat it as users.php?page=$1