How to generate url and read on Codeigniter?

How do I create a dynamic url in Codeigniter ?.

I am following the table that stores the basic information about the article:

 1. ID
 2. Article Title
 3. Content
 4. Tags
 5. Status
 6. Create Time
 7. Update Time
 8. Author ID
 9. Status


Now I want to know how to create a dynamic url that contains Page ID

and Article Title


for example

Now above the sample URL, I create a URL with ID

and Article Title


  • ID to get the content of the article (when the user clicks on the article, gets the content from the ID).
  • Article title for a secure URL.

but I don't want to show ID

from url and get content when user is redirected to detail page.


View: home_page.php

<div id="body">
        <?php for($i=0; $i<count($blogPosts); $i++): ?>
            <div class="post-preview">
                    $postID = $blogPosts[$i]->id; // Get post ID
                    $postTitle = $blogPosts[$i]->title; // Get post Title
                    $urlTitle = preg_replace('/\s+/', '-', $postTitle); // Replacing space with dash
                    echo anchor('content/'.$postID.'/'.$urlTitle, $postTitle, array('title' => $postTitle)); // Get post URL
                    echo "<br />";
        <?php endfor; ?>


Controller: home.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

    public function index() {

        $this->load->model("HomeModel"); // Load HomeModel
        $data['blogPosts'] = $this->HomeModel->blogPosts(); // Get blog posts data from blogPostsData function
        $this->load->view("home_page", $data);




Model: HomeModel.php

class HomeModel extends CI_Model {

    public function blogPosts() {

        $query = $this->db->get();
        return $query->result();




After coding When I find the anchor link I get this url:



How to hide index.php

, content

and 1

from the URL.

One more thing, when I clicked on the link, I get the error:

An Error Was Encountered 
The URI you submitted has disallowed characters.


Any help would be appreciated!


source to share

3 answers

You can use routes

for this purpose

To remove index.php

use .htaccess

with the following code

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


and in config.php

$config['uri_protocol'] = 'AUTO';


To remove content from url, remove content from anchor in view

echo anchor($postID.'/'.$urlTitle, $postTitle, array('title' => $postTitle)); // Get post URL


I don't think you should remove 1

from url. If you remove it from the url, then how can you identify your post. Therefore it will be helpful to store it in the url. Or, if you want to remove, add this id at the end of the post header, like



and blow up the last element for id post

An Error Was Encountered 
The URI you submitted has disallowed characters.


you are getting this error because in your url you added !

. To make your application safe, CI prevents special characters from appearing from the URL. Therefore, it would be better not to use special characters in the URL other than _

and -


On your controller

class Content extends CI_Controller {
    function index($id, $name)
        echo $name;// this contain book name 
        echo $id;// this contain book id

        //your code


now on your routes application/config/routes.php





If you store the pool (article title for a secure url) in a database, you don't need to expose the ID at all. This will involve adding a column to the table.

You can automate the creation of the slug when you create the article using url_title()

as detailed here .



<a href="<?php echo base_url()?>index.php/controller_name/method_name/<?php echo $item['name'] ?>/<?php echo $item['id'] ?>" alt="">click here to view</a>


So your url looks like id

In the controller

function method_name($name, $id)
    echo $name;// this contain book name 
    echo $id;// this contain book id

    //Simply you can use this to get all data
    $result = $this->Model_name->get_book_details($id);//in model $id hold the id of book




All Articles