0

I want to Create SEO Friendly URLs With Htaccess Mod Rewrite like this:

http://localhost/try/pagination/page/4

but now output showing when we click on next button

http://localhost/try/pagination.php/page/pagination.php/page/2

My php code is :

<?php

//include 'config.php';
define('DB_HOST', 'localhost');
define('DB_NAME', 'wr');
define('DB_USER','root');
define('DB_PASSWORD','');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());

echo $_GET['page'];

$rowsPerPage = 1;

if(isset($_GET['page']))
{
$pageNum= $_GET['page'];
}
else
$pageNum = 1;
$previousRows =($pageNum - 1) * $rowsPerPage;
$query = "SELECT * FROM articles LIMIT $previousRows, $rowsPerPage";
$result = mysql_query($query) or die('Error couldn\'t get the data').mysql_error();
echo "<table border=1>\n";
echo "<tr><th>ID</th><th>Name</th><th>Password</th><th>Perm</th><th>Email</th>
<th>Date</th></tr>";
while(list($id,$title,$content) = mysql_fetch_array($result))
{
echo "<tr><td>$id</td><td>$title</td><td>$content</td></tr>";
}
echo '</table>';
$query = "SELECT COUNT(title) AS numrows FROM articles";
$result = mysql_query($query) or die('Error, couldn\'t get count title=\"$page\"').mysql_error();
$row = mysql_fetch_assoc($result);
$numrows = $row['numrows'];
$lastPage = ceil($numrows/$rowsPerPage);
echo $lastPage;
$phpself = "pagination.php";
echo $phpself;
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$phpself?page=$page\" title=\"Page $page\">[Back]</a> ";
$first = " <a href=\"$phpself?page=1\" title=\"Page 1\">[First Page]</a> ";
}
else
{
$prev = ' [Back] ';
$first = ' [First Page] ';
}
if ($pageNum < $lastPage)
{
$page = $pageNum + 1;  
$next = " <a href=\"$phpself/page/$page\" title=\"Page $page\">[Next]</a> ";
$last = " <a href=\"$phpself?page=$lastPage\" title=\"Page $lastPage\">[Last Page]</a> ";
}
else
{
$next = ' [Next] ';
$last = ' [Last Page] ';
}
echo $first . $prev . " Showing page <bold>$pageNum</bold> of
<bold>$lastPage</bold> pages " . $next . $last;
?>

.htaccess file code is:

Options +FollowSymLinks

RewriteEngine On
RewriteRule ^pagination/page/([0-9]+)\.html$ pagination.php?page=$1

Next requirement is if we are pass the two variable

http://localhost/try/pagination/tag/php/page/2

How I can change in Php code and .htaccess file?

gp_sflover
  • 3,460
  • 5
  • 38
  • 48
ashwani
  • 19
  • 3
  • `mysql_` \*ahem\* : https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – CD001 Jun 07 '17 at 13:14
  • `http://localhost/wr/try/pagination.php/page/pagination.php/page/2` is that *really* the URL? 2x `pagination.php` in it? ... and where did you find that pattern: `RewriteRule ^pagination/([0-9]+)\.html$ pagination.php?page=$1` - that's just rewriting things like *pagination/123.html* and doesn't seem to fit your requirements at all... – CD001 Jun 07 '17 at 13:18
  • now update the post and please give me solution – ashwani Jun 07 '17 at 13:38
  • Tricky since I'm having a hard time working out exactly what you're trying to achieve, do you want an URL like `http://localhost/try/pagination/page/4` to point to `http://localhost/try/pagination.php?page=4` ? – CD001 Jun 07 '17 at 14:06
  • I want to convert this seo friendly url : http://localhost/try/pagination.php?page=4 into this : http://localhost/try/pagination/page/4 – ashwani Jun 07 '17 at 14:16
  • aha, then put that in your question – Joram Jun 07 '17 at 14:59

1 Answers1

0

Assuming your .htaccess file is in the docroot - and that you mean you want your URLs to appear in the "SEO Friendly" format of /try/pagination/page/4

RewriteEngine On
RewriteRule ^try/pagination/page/([0-9]+)/?$ /try/pagination.php?page=$1 [L]


Then you need to update the code that generates the URLs; for instance, replace:

$prev = " <a href=\"$phpself?page=$page\" title=\"Page $page\">[Back]</a> ";

with:

$prev = " <a href=\"/try/pagination/page/{$page}\" title=\"Page $page\">[Back]</a> ";
CD001
  • 8,332
  • 3
  • 24
  • 28
  • I replace the code not show the correct output If i click the next button Show Output http://localhost/wr/try/pagination/page/2 Next Output Show : http://localhost/wr/try/pagination/page/pagination/page/3 Page Cannot Find because (pagination/page/) Again Add So Show the Output Page cannot find – ashwani Jun 07 '17 at 14:42
  • Where has that `/wr/` come from? Have you updated the code that generates the *Next Page* link? What happens if you just type `http://localhost/try/pagination/page/3` in the address bar? – CD001 Jun 07 '17 at 14:57
  • wr and try is folder name (You can ignore it) So I am show the Step by step Out put page wise Go to 2nd page Show the link http://localhost/try/pagination/page/2 Output is OK and now GO to 3rd page Show the Output is http://localhost/try/pagination/page/pagination/page/3 Now show the output is : page cannot find Due to ( pagination/page/) ===again Add ------ Main problem is if we are go to next page so add the (pagination/page/) again and again Like that Go to 3 rd page Show url :http://localhost/try/pagination/page/pagination/page/3 – ashwani Jun 08 '17 at 05:31