【发布时间】:2012-05-11 04:19:26
【问题描述】:
我正在尝试使用 Codeigniter 创建一个内容管理系统。我有一个显示文章标题列表的页面。当任何用户点击任何文章标题时,都会将用户带到单独页面中该文章的详细信息。
我正在使用以下代码显示文章标题列表:
<a href="<?php echo base_url(); ?>article/<?php echo $row['article_id']; ?>">
<?php echo $row['article_title']; ?></a>
当任何用户点击上面的链接时,它会获取 article_id 并转到以下控制器
控制器:
function index($id){
$this->load->model('mod_articles');
$data['records']=$this->mod_articles->list_articles($id);
$this->load->view('view_article',$data);
}
型号:
function list_articles($id)
{
$this->db->select('*');
$this->db->from('article');
$this->db->where('article_id', $id);
$query = $this->db->get();
return $query->row_array();
}
现在当我显示结果时,在浏览器的地址栏中,链接看起来像这样-
localhost/cms/article/1 //<< here localhost/cms/ is my base_url
根据这个website,良好的SEO友好URL示例是http://www.mysite.com/joomla-seo-tips。
现在我的问题是如何让我的 URL 看起来像 localhost/cms/article/my-article-title 而不是在链接末尾显示文章的 ID?
要实现这一点,我应该查询我的文章标题而不是文章 ID,还是有一些更好的方法可以做到这一点?
请分享您对此的想法。
在此先感谢 :)
编辑
那么,在保存我的内容之前,我应该运行以下代码获取$article_slug 然后保存它吗?
function create_slug($string){
$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
return $slug;
}
$article_slug= create_slug('My Name '); // as you suggested I will create a new column for this
【问题讨论】:
-
如果你的 url 看起来像这样……怎么办? localhost/cms/article/1/my-article-title
-
我猜也可以。但是怎么做呢?谢谢:)
标签: php codeigniter seo