[eluser]Stuey[/eluser]
I'm currently very new to all of this and have been tinkering with the very same approach to my own little blog.
I've basically taken the approach of setting up a route in config/routes.php that goes something along the lines of:
Code:
$route['blog/(:num)'] = "blog/view/$1";
$route['blog/(:num)/(:any)'] = "blog/view/$1/$2";
Then in my controller I have this:
Code:
function view($post_id=null,$sef_url="")
{
/* Don't forget this relies on a custom ROUTE */
// Fetch post by id or throw 404
if (!isset($post_id)) {
show_404($this->uri->uri_string());
}
$this->load->model('blog_model');
if ($this->db->conn_id == "")
{
show_error("Could not connect to the database with the current settings.");
}
// Fetch post by id or throw 404
if (!$row = $this->blog_model->get($post_id)) {
show_404($this->uri->uri_string());
}
// Do a 301 redirect if sef_url is not a match
if ($sef_url != $row['sef_url']) {
redirect(site_url('/blog/view/' . $post_id . '/' . $row['sef_url']), 'location', 301);
}
// All is well. Display post.
$this->data['post'] = $row;
$this->load->view('post_view', $this->data);
}
Basically my URI structure contains the post ID, so that you could enter "/blog/view/123/whatever-slug-line-i-want" and it would still go to the correct article.
If the slug/SEO friendly URL (sef_url in my case) doesn't match, it redirects to the correct post anyway. You could replace the ID in my example with a alias lookup.
Probably could be done better than this, but I'm still learning.
Hope that helps.