CodeIgniter Forums
SEO implement? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: SEO implement? (/thread-29952.html)



SEO implement? - El Forum - 04-27-2010

[eluser]Iwasignited[/eluser]
Hi everyone,

I have a question related to SEO. I want to turn my url from 'post/view/1' to 'post/view/title-of-the-first-post', I created a column called alias in my post table but I'm not sure what to do next. Please help me. Thanks.

Best regards


SEO implement? - El Forum - 04-27-2010

[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. Smile

Hope that helps.


SEO implement? - El Forum - 04-27-2010

[eluser]Iwasignited[/eluser]
that definitely helps. Thank you again


SEO implement? - El Forum - 04-27-2010

[eluser]pickupman[/eluser]
Taking the idea from Phil Sturgeon's PyroCMS and similar feature in Wordpress, is create a slug field. When I create a new article, I have the slug auto generated from the title of the post. I check the DB if slug already exists when saving the page. Then I use the route
Code:
$routes['blog/(:any)'] = 'blog/view/$1';

Then in my blog/post method I first lookup the slug parameter in the DB, and if nothing is returned I check for a post id. If that fails, I just load the default "home" slug.


SEO implement? - El Forum - 04-27-2010

[eluser]paulc010[/eluser]
A method I've used for news stories (never going to be that many of them, so the limitations are acceptable):

Code:
function post($uri = 1)
    {
        // The $id could be a post_id or a post_slug
        $id=$this->posts_model->get_post_id_from_slug($uri);

        if ($id === 0) :
            // It's not a slug it might be a numeric post id
            $id=$uri;

            // Does the post exist?
            if ($this->posts_model->is_post($id)) :
                // Let's see if we have a nice name for this page
                $slug = $this->posts_model->get_post_slug($id);
                if ($slug != $uri) :
                    redirect(base_url().'news/post/'.$slug, 'location', 301);
                endif;
            endif;
        endif;

        // Get the data and display it
    }

The 'slug' in the post table has to be unique, so you would need to enforce that.....

Paul


SEO implement? - El Forum - 04-27-2010

[eluser]pickupman[/eluser]
Code:
redirect(base_url().'news/post/'.$slug, 'location', 301);

Probably should be:
Code:
redirect(site_url('news/post/'.$slug), 'location', 301);

In case you ever change enable/disable index.php moving files to different server configs, site_url() works better, as it figures out whether you have a index.php or not.


SEO implement? - El Forum - 04-27-2010

[eluser]Stuey[/eluser]
[quote author="Iwasignited" date="1272394230"]that definitely helps. Thank you again[/quote]
Thinking back, it was one of Phil's posts where I got that code I think.

I found it useful to keep the post/article ID in the URL (just before the slug URI) so that if someone mistyped the slug when referring/linking it would still serve up the correct article as well as allowing duplicate slugs (not that such things would happen often tho).

Paul's approach is good also, keeps it simple and logical imo.