dongza3124 2017-06-07 13:09
浏览 38

使用Htaccess Mod Rewrite创建SEO友好URL

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>
";
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?

  • 写回答

1条回答 默认 最新

  • dqxm14187 2017-06-07 14:28
    关注

    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> ";
    
    评论

报告相同问题?