Genocide_Hoax
Genocide_Hoax

Reputation: 853

CodeIgniter URL - 404 Error

I am in the threshold of exploring the CodeIgniter Framework, and I am facing a very basic problem here. I have a controller named pages, and it has a function called view.

Problem 1: Now when I type example.com/myfolder/ I do land to my page. But when I try something like example.com/myfolder/index.php/pages/view or example.com/myfolder/pages/view, it gives me a 404 Error.

Problem 2: I need to remove the index.php from the URL. Please rectify the .htaccess pasted below.

Controller

class Pages extends CI_Controller {

    public function index() {
        $this->view("home");
    }

    public function view($page = 'home') {
        if (!file_exists('application/views/pages/' . $page . '.php')) {
            // Whoops, we don't have a page for that!
            show_404();
        }
        $data['title'] = ucfirst($page); // Capitalize the first letter
        $data['company_name'] = "Demo Company 2013";
        $this->load->view('templates/header', $data);
        $this->load->view('pages/' . $page, $data);
        $this->load->view('templates/footer', $data);
    }

}

Routes

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

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /myfolder/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

config

$config['index_page'] = '';
$config['base_url'] = 'http://example.com/myfolder/';

Update

This might be important. I am running my site on a shared windows hosting, which is using IIS. So I suppose my .htaccess needs to be converted to web.config.

UPDATE WEBCONFIG ADDED

Now, I have added the web.config from the link provided in one the answers. Thanks a lot; it was really helpful, but still I am getting the same old 404 Error. I tried changing the following my configuration file

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

But now I am getting errors about

<!doctyle HTML>

How can I fix this?

Upvotes: 0

Views: 3252

Answers (3)

Mudshark
Mudshark

Reputation: 3253

I'm guessing this line is serving the 404:

if (!file_exists('application/views/pages/' . $page . '.php')) {

Try replacing it with:

if (!file_exists(APPPATH.'views/pages/' . $page . '.php')) {

Upvotes: 0

Dan
Dan

Reputation: 425

This is what I normally use that works on a number of servers:

DirectoryIndex index.php

<IfModule mod_rewrite.c>

  RewriteEngine on
  RewriteBase /myfolder
  RewriteCond $1 !^(index\.php|user_guide|robots\.txt)
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d 
  RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

</IfModule>

<IfModule !mod_rewrite.c>
  ErrorDocument 404 index.php
</IfModule> 

Depending on your server configuration you may need to remove the ? after index.php,

Change:

RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

To:

RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Or else you may get a 404 No input specified error

Upvotes: 0

Jerin K Alexander
Jerin K Alexander

Reputation: 301

You can write your .htacces like this

Options -Indexes

Options +FollowSymLinks

DirectoryIndex index.php

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !index.php
    RewriteRule (.*)\.php$ index.php/$1
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [QSA,L]

</IfModule>

And check your config.php file and do the changes like this.

$config['base_url'] = '';

$config['index_page'] = '';

Upvotes: 2

Related Questions