Tutorials

Article Content:Codeigniter Clean URL

Codeigniter Clean URL

SEO, not a choice, if you have a website then you want people to see it, because it is not a desktop application for someone only, that will make web developers ran into problems because the most popular PHP frameworks and CMS don’t provide support SEO at their basic tools or modules, I also don’t know why ?

so you will figure out this with some googling, if you are on Drupal or WordPress you will find many modules or plugins, also the same situation with frameworks, every framework has his developers to provide this support by many hacks, libraries, third parties, and in this tutorial I will provide my steps to make Codeigniter SEO friendly URL or to clean Codeigniter URL.

Let’s go and follow me.

 

1- Remove index.php

The first basic step after install Codeigniter is to remove the default path – index.php- from URL, to do this create text file, rename as .htaccess, put this code and save

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

 

So if URL looks like this

example.com/index.php/news/article/my_article
After using htacess file will be like this
example.com/ news/article/my_article

2- Use slug URL

If this term being some weird to you no problem I going to explain now, but first look at these two links below and tell me what is look like more readable?

www.example.com/blog/view/7
www.example.com/blog/view/how-to-improve-skills

 

As I expect, the second is more readable because at the first glance you will know what you read and what this article talk about without opening the link, also the search engine doesn't know what is number 7 ,but it will know and understand this sentence ‘how-to-improve-skills’

To apply slug on your website you have many choices (libraries, helpers, etc.), but in my opinion, you shouldn’t use any of them, because it is quite simple to apply.

I suppose you have table like that with column called ‘slug’, if not , add new slug column

CREATE TABLE IF NOT EXISTS `ci_news` (

  `ne_id` int(11) NOT NULL AUTO_INCREMENT,
  `ne_title` varchar(300) NOT NULL,
  `ne_slug` varchar(255) NOT NULL,
  `ne_desc` text NOT NULL,
  `ne_img` varchar(255) NOT NULL,
  `ne_created` varchar(50) NOT NULL,
  PRIMARY KEY (`ne_id`)

) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Then validate unique title in (controller/add) function to avoid duplicated title and slugs like this

$this->form_validation->set_rules('ne_title', 'Title', 'required||is_unique["ci_news.ne_title"]
');

 

And in your (model/create) function, this will replace spaces with underscores and save the slug in ne_slug column

$slug = url_title(convert_accented_characters($this->input->post('ne_title')), '_', true)
$this->db->set('ne_slug',slug);

 

Note:
if you want to make articles with the same title name, you can use another trick which is added DateTime to slug –like WordPress-this will make it unique always.

In your HTML views, your link will look like

<a href="base_url() ?>blog/ news/$news['ne_slug'] ?>" class="btn">ReadMorea>

If someone clicks on this link, you should prepare your query to select by slug rather than id

 
function get_post($slug) {

    $query = $this->db->get_where('ci_news', array('ne_slug' => urldecode($slug)));

    if ($query->num_rows() !== 0) {

        return $query->row();

    } else {

        return FALSE;

    }

}

 

3- Choose the right name

When you build your website, be accurate and use the exact name for your controller and function, because as you might know Codeigniter use this controllers and methods to generate URL, as example if you want to create news website you should naming controllers like this

News_category_controller

News_controller

Also, you show naming news details like this

Function news_dtails()

{

}

 

The result

Your link will be like this

 



Leave a Reply

Your email address will not be published.


Notify me of followup comments via e-mail.
You can also Subscribe without commenting.