0

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?

3 Answers 3

2

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'] = '';
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply Jerin. But it didn't work as expected. I am still getting 404 on both the url's Mentioned.
Can you remove the route settings and try the url manually? $route['default_controller'] = 'pages'; $route['404_override'] = ''; like this
There is nothing you need to focus other than route.php file. The htaccess is perfectly working for me.
0

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

1 Comment

Sorry, I've never run Codeigniter on IIS, this came up in a quick search : stackoverflow.com/questions/12808063/…
0

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')) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.