【问题标题】:How to set permalink of your blog post according to date and title of post?如何根据帖子的日期和标题设置博客帖子的永久链接?
【发布时间】:2011-11-24 03:10:15
【问题描述】:

我有这个网站 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/ 。

我正在使用以下函数来获取这些帖子...

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;
}

我正在使用此代码在我的 index.php 页面上显示它...

<?php
    if (isset($_GET['id'])) :
        $obj -> get_content($_GET['id']);
    else :
        $obj -> get_content_summary();
    endif;
?>

任何建议如何做到这一点?我们可以通过使用 .htaccess 来做到这一点吗?

【问题讨论】:

  • 如果您的服务器上启用了 mod_rewrite,您可以在 .htaccess 中使用它。
  • 但这只是一篇文章?或者告诉我该怎么做? wordpress 是怎么做到的?
  • 我从未使用过wordpress,所以我不知道。你熟悉正则表达式吗?您可以使用带有正则表达式捕获组的重写规则来执行此操作。
  • 看到这个,我想达到这样的目标askapache.com/htaccess/…
  • 您也可以考虑查看webmasters.stackexchange.com

标签: php mysql .htaccess


【解决方案1】:

使用 mod_rewrite 的不幸之处在于,您以 url 的形式提供的数据并不是查询数据库的最佳方式。但是你仍然有年、月、日和标题变量,所以你需要重写你的 get_content 函数来查询类似的东西(取决于你的日期是如何存储在数据库中的。):

select * from cms_content 
   WHERE date='mktime(0,0,0,$month,$day,$year)'
   AND title='$title'
 ORDER BY id DESC

.htaccess 类似于:

RewriteRule ^(.*)/(.*)/(.*)/(.*)$  index.php?year=$1&month=$2&day=$3&title=$4

【讨论】:

  • 实际上,我没有年月日列。相反,我有一个名为“日期”的列作为时间戳。那么现在该怎么做呢?
猜你喜欢
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
  • 1970-01-01
  • 2011-03-28
  • 2019-07-24
  • 2012-04-06
相关资源
最近更新 更多