如何按照日期和岗位的标题组为博客永久链接? [英] How to set permalink of your blog post according to date and title of post?
问题描述
我有这个网站 http://www.finalyearondesk.com 。我的博客张贴的链接设置这样的.. http://www.finalyearondesk.com/index .PHP?ID = 28 。我希望它这样设置... finalyearondesk.com/2011/09/22/how-to-recover-ubuntu-after-it-is-crashed/。
I am having this website http://www.finalyearondesk.com . My blogs post link are set like this.. http://www.finalyearondesk.com/index.php?id=28 . I want it to set like this ... finalyearondesk.com/2011/09/22/how-to-recover-ubuntu-after-it-is-crashed/ .
我使用下面的函数来获得这些职位...
I am using the following function to get these posts...
function get_content($id = '') {
if($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * from cms_content WHERE id = '$id'";
$return = '<p><a href="http://www.finalyearondesk.com/">Go back to Home page</a></p>';
echo $return;
else:
$sql = "select * from cms_content ORDER BY id DESC";
endif;
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) != 0):
while($row = mysql_fetch_assoc($res)) {
echo '<h1><a href="index.php?id=' . $row['id'] . '">' . $row['title'] . '</a></h1>';
echo '<p>' . "By: " . '<font color="orange">' . $row['author'] . '</font>' . ", Posted on: " . $row['date'] . '<p>';
echo '<p>' . $row['body'] . '</p><br />';
}
else:
echo '<p>We are really very sorry, this page does not exist!</p>';
endif;
}
和我用这code到dispaly它在我的index.php页面...
And I am using this code to dispaly it on my index.php page...
<?php
if (isset($_GET['id'])) :
$obj -> get_content($_GET['id']);
else :
$obj -> get_content_summary();
endif;
?>
任何建议如何做到这一点?而我们能做到这一点,使用的.htaccess?
Any suggestions how to do this? And can we do this by using .htaccess?
推荐答案
有关使用mod_rewrite不幸的事情是,你提供一个URL的表单中的数据不是查询数据库的最佳方式。但没有少你的年,月,日和标题变量,所以你需要重写你的get_content功能来查询soomething像(取决于你如何日期存储在数据库中。):
The unfortunate thing about using mod_rewrite is that the data you are supplying in the form of a url is not the best way to query a database. But none the less you have year, month, day and title variables so you will need to rewrite your get_content function to query soomething like (depending on how you date is stored in the database.):
select * from cms_content
WHERE date='mktime(0,0,0,$month,$day,$year)'
AND title='$title'
ORDER BY id DESC
的.htaccess会是这样的:
.htaccess would be something like:
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ index.php?year=$1&month=$2&day=$3&title=$4
这篇关于如何按照日期和岗位的标题组为博客永久链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!