关于`.htaccess,slugs和重写网址的简单问题。
如果我有一篇文章,如何用数据库中的内容替换地址栏中的整个网址?
例如在我的网址中我有/article.php?id=1
在本例中id为1的文章的id中,我在数据库中有一个名为“art_url”的字段名称,其值为/article/my-url
如何让它成为地址而不是/article.php?id=1
?
当我尝试使用此网址dev.mywebaddress.com/article/my-url/
时出现PHP错误。我的PHP中是否存在使用htaccess文件的问题?这是我的代码:
<?php
if(!empty($_GET['art_url']))
{
$sql = "
SELECT articles.id AS articleid, articles.art_title AS title, articles.art_url AS art_url, articles.art_content AS content, campaigns.id, campaigns.camp_title AS camptitle
FROM articles
LEFT JOIN campaigns ON articles.art_campaign = campaigns.id
";
}
// then do the query, etc....
$results = $db->query($sql);
if($results->num_rows) {
While($row = $results->fetch_object()) {
$camptitle = nl2br(htmlentities($row->camptitle));
$title = nl2br(htmlentities($row->title));
$content = nl2br($row->content);
$art_url = ($row->art_url);
echo "
<H2>{$camptitle}</H2>
<H3>{$title}</H3>
<p>{$content}</p>
";
}
} else {
echo 'No Results';
}
?>
.htaccess文件
# Turn Rewrite Engine On
RewriteEngine On
#RewriteRule ^article/([0-9a-zA-Z-/]+) article.php?art_url=$1
答案 0 :(得分:0)
您可以在PHP代码中添加301重定向。查找art_slug
的值,然后查看..
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.yourdomain.com/".$art_slug);
?>
您的.htaccess文件可以设置为使用以下方式获取XXX / YYY:
RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?firstSlug=$1&secondSlug=$2 [NC,L,QSA]
然后你会在index.php收到firstSlug = XXX和secondSlug = YYY