我想用Htaccess Mod Rewrite创建SEO友好的URL,如下所示:
http://localhost/try/pagination/page/4
但现在输出显示我们点击下一个按钮
http://localhost/try/pagination.php/page/pagination.php/page/2
我的PHP代码是:
<?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文件代码是:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^pagination/page/([0-9]+)\.html$ pagination.php?page=$1
下一个要求是我们是否传递了两个变量
http://localhost/try/pagination/tag/php/page/2
我如何更改Php代码和.htaccess
文件?
答案 0 :(得分:0)
假设您的.htaccess
文件位于docroot中 - 并且您的意思是您希望您的网址以 / try / pagination / page的“SEO Friendly”格式显示/ 4 强>
RewriteEngine On
RewriteRule ^try/pagination/page/([0-9]+)/?$ /try/pagination.php?page=$1 [L]
然后您需要更新生成URL的代码;例如,替换:
$prev = " <a href=\"$phpself?page=$page\" title=\"Page $page\">[Back]</a> ";
使用:
$prev = " <a href=\"/try/pagination/page/{$page}\" title=\"Page $page\">[Back]</a> ";