black_belt
black_belt

Reputation: 6799

Making a SEO friendly URL for a CMS

I am trying to create a Content Management System using Codeigniter. I have a page where I display a list of article titles. When any user click on any of the article titles it will take the user to the details of that article in a separate page.

I am displaying the article title list using the following code:

 <a href="<?php echo base_url(); ?>article/<?php echo $row['article_id']; ?>">
 <?php echo $row['article_title']; ?></a>

When any user click on the above link it takes the article_id and go to the following controller

Controller:

function index($id){

   $this->load->model('mod_articles');
    $data['records']=$this->mod_articles->list_articles($id);
    $this->load->view('view_article',$data);
}

Model :

  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();

    }  

Now when I display the result, in the browser's address bar the link looks like this-

  localhost/cms/article/1         //<< here localhost/cms/ is my base_url

According to this website , an example of good SEO friendly URL is http://www.mysite.com/joomla-seo-tips .

Now my question is how to make my URL look like localhost/cms/article/my-article-title instead of showing the id of the article at the end of link?

To acheive this should I query for my article titles instead of article ids or there are some better ways to do that?

Please share your ideas on this.

Thanks in Advance :)

Edit

So, before saving my content should I run following code get the $article_slug and then save it?

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

Upvotes: 0

Views: 1845

Answers (2)

JamesOR
JamesOR

Reputation: 1168

Your 'article' table can include an additional field ('slug' is a popular name for it so in your example maybe 'article_slug') that is used as both a part of the URL and a field to query against. Typically the CMS will show 'slug' as an editable field but pre-populate it with the value of the page title transformed (typically by lowercasing the title, converting spaces to dashes and cleaning up any URL unfriendly characters like quotes).

Upvotes: 1

No Results Found
No Results Found

Reputation: 102735

When you save the record, give it a URL slug by running url_title() on the title (this should be a unique column, make sure to check for duplicate entries when saving).

url_title("My page title!") should return something like my-page-title, see the url helper. I usually run strtolower on slugs as well, looks like CI allows caps.

When you query for the content, get it by slug, something like:

$this->db->where('slug', $this->uri->segment(1))->get('articles'); 

And of course use the slug for your links instead of ids.

Upvotes: 2

Related Questions