Search code examples
phpcodeignitercodeigniter-routing

MVC index.php in every url


I started using codeigniter for the first time today although I have used asp.net mvc before. I am wondering why my URL needs the index.php at the start all of the time ?

example:

http://localhost/index.php/mozilla-developing-chromecast-competitor-that-runs-firefox-os-report

There is no function called index.php in my controller. I am pretty sure this is a silly question but I have been trying to fix it for over an hour and am still clueless.

routes:

$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/home';

Controller

class Pages extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->model('pages_model');
}

public function home()
{
    $data['blog'] = $this->pages_model->get_blog();
    $data['title'] = 'Blog archive';

    $this->load->view('templates/header', $data);
    $this->load->view('pages/home', $data);
    $this->load->view('templates/footer');
}

public function about()
{
    $data['title'] = 'About us';

    $this->load->view('templates/header', $data);
    $this->load->view('pages/about', $data);
    $this->load->view('templates/footer');
}

public function view($slug)
{
    $data['blog_item'] = $this->pages_model->get_blog($slug);

    if (empty($data['blog_item']))
    {
        show_404();
    }

    $data['title'] = $data['blog_item']['title'];

    $this->load->view('templates/header', $data);
    $this->load->view('pages/view', $data);
    $this->load->view('templates/footer');
}
}

HTAaccess

Deny from all

Solution

  • Take a look at URL-Routing in codeigniter: http://www.technicalkeeda.com/details/how-to-create-seo-friendly-url-using-php-codeigniter also check this please and ensure your .htaccess in WebRoot is fine. http://kevinthompson.info/blog/2011/03/04/completely-remove-index-php-from-expressionengine-urls.html . This should help you out.

    Or the basic setup:

    Options +FollowSymlinks
    RewriteEngine on
    
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # otherwise forward it to index.php
    RewriteRule . index.php
    

    Or

    <IfModule mod_rewrite.c>
    
    # Enable Rewrite Engine
    # ------------------------------
    RewriteEngine On
    RewriteBase /
    
    # Redirect index.php Requests
    # ------------------------------
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{THE_REQUEST} !/system/.*
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
    
    # Standard ExpressionEngine Rewrite
    # ------------------------------
    RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
    
    </IfModule>