我在我的项目中使用了 smarty PHP。
通常我在 smarty 中使用以下代码来创建链接并将它们指向它们的特定 URL:
在我的 smarty 模板页面中:
{section name=title loop=$title}
<li><a class="nav" href="index.php?url={$title[title].url}">{$title[title].title}</a></li>
{/section}
上面的代码将生成以下 URL:
http://domain.com/index.php?url=somename.html
somename.html
存储在mysql数据库中。
在我的 index.php 文件中,我得到了 url=someone.html 的详细信息,如下所示:
在我的 smarty 模板页面中:
{if isset($smarty.get.url)}
{$body}
{/if}
在我的 php 页面中:
if(isset($_GET['url']))
{
include "config/connect.php";
$url = preg_replace('#[^0-9]#i', '', $_GET['url']);
$url=mysqli_real_escape_string($db_conx, $_GET['url']);
if ($stmt = mysqli_prepare($db_conx, "SELECT id, url, title, body FROM pages WHERE url=?")) {
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "s", $url);
/* execute query */
mysqli_stmt_execute($stmt);
/* bind result variables */
mysqli_stmt_bind_result($stmt, $id, $url, $title, $body);
/* fetch value */
mysqli_stmt_fetch($stmt);
$pageurl=$_GET['url'];
/* close statement */
mysqli_stmt_close($stmt);
}
}
/* close connection */
mysqli_close($db_conx);
$smarty->assign('id', $id);
$smarty->assign('url', $url);
$smarty->assign('title', $title);
$smarty->assign ('body', $body);
到目前为止一切正常。
我的问题是:
我正在尝试创建 SEO 友好的 URL。
例子:
http://domain.com/index.php?url=somename.html
到
http://domain.com/somename.html
我的 htaccess 文件中有这段代码:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/]+).html$ index.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+).html/$ index.php?url=$1
上面的代码将允许我转换 http://domain.com/index.php?url=somename.html
到 http://domain.com/somename.html
但是,
当我在浏览器中查看http://domain.com/somename.html
时,我得到一个空白页面,这意味着我没有得到somename.html 的相关信息
。
例如,如果我在浏览器中运行 http://domain.com/index.php?url=somename.html
,我会得到 {$body}
我页面中的 somename.html
或 something.html
等。
但是如果我在浏览器中运行 http://domain.com/somename.html
,我什么也得不到,因为我似乎无法访问 $url = preg_replace( '#[^0-9]#i', '', $_GET['url']);
我需要对 smarty PHP 或 htaccess 文件做些什么才能完成这项工作吗?
如有任何帮助,我们将不胜感激。
最佳答案
问题出在你的重写规则上
RewriteRule ^([a-zA-Z0-9-/]+).html$ index.php?url=$1
.html
不包含在 ()
中,因此 $1
包含 .html 之前的所有内容
例如http://domain.com/somename.html
被重写为 http://domain.com/index.php?url=somename
关于php - 使用 smarty php 的 SEO 友好 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23782582/