-1

I need to make custom URL in Codeiginter to display links from

domain.com/news/id/1 to domain.com/article-name.html

i want it dynamic to all news id not just one link

can i do it in codeiginter or not ?

fergany
  • 127
  • 1
  • 1
  • 4

2 Answers2

0

Yes you can, This is called URL rewrite. I can explain here but the official documentation is far better.

here it is: http://ellislab.com/codeigniter/user-guide/general/urls.html

Krimson
  • 7,386
  • 11
  • 60
  • 97
0

In your config.php file do this:

$config['url_suffix'] = '.html'; // this line might actually not be necessary, i forget

In routes.php set this:

$route['article-name.html'] = "news/id/1";

Let me know if you get any errors.

UPDATE

Step 1: Set your default controller in routes.php

$route['default_controller'] = 'get_article';

Step 2: Set your config.php to put .html on the end

$config['url_suffix'] = '.html';

Step 3: Create the Controller

// path:   /codeigniter/2.1.4/yourAppName/controllers/get_article.php

<?php
class Get_article extends CI_Controller {

    public function index($article = '')
    {
        // $article will be "article-name.html" or "article2-name.html"
        if($article != '')
        {
            $pieces = explode('.', $article);

            // $pieces[0] will be "article-name"
            // $pieces[1] will be "html"

            // Database call to find article name
            // you do this

            $this->load->view('yourArticleView', $dbDataArray);
        }
        else
        {
            // show a default article or something
        }
    }
}
?>
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77