0

Followed a tutorial to remove index.php from the URLs in Code Igniter.

It works now for my default_controller, but not for other pages(or views) properly.

I have one controller, Pages, and it has one method, View($page = 'home'), to load pages with other content based on the parameter passed.

If I type localhost/dev into URL - I land on my home page, which is correct.

If I type localhost/dev/aboutus - I receive 404. It only works if I type localhost/dev/pages/view/aboutus.

What I wanted to happen is by typing localhost/dev/aboutus it would show me the AboutUs view.

Routes.php

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

Pages.php (controller)

<?php
    class Pages extends CI_Controller
    {
        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

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

.htaccess file (located in /dev/ folder)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /dev/

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule> 
6
  • Your htaccess rewrites /dev/ path but routes.php points to a controller named dev - your controller is pages not dev Commented Oct 2, 2012 at 19:07
  • Are you sure mod_rewrite is enabled? What OS is your server? Commented Oct 2, 2012 at 19:15
  • @jtheman Removed the /dev/ part. Still doens't work. Commented Oct 2, 2012 at 19:16
  • @JordanArseno I'm running it off Wamp at the moment. Later I will upload it to my Linux host online. Commented Oct 2, 2012 at 19:17
  • Did you just remove it from the right part or both? Also the subject of this question is wrong as the issue has not to do with index.php but is related to routing/rewrites. Commented Oct 2, 2012 at 19:23

2 Answers 2

1

This route $route['dev/(:any)'] = "dev/pages/view/$1"; is a little fishy because CodeIgniter routes should not include the project name (dev in your case). They begin on the controller level, not the project one.

The route you have written would imply that if a user typed http://localhost/dev/dev/something (yes, two dev's) they would be routed to a controller dev.php and into a function with prototype: function('view', 'something'){}

Why so complicated? You should make all of your main page names into controllers. Routes should only be touched for special cases.

Create a file aboutus.php in application/controllers with contents:

<?php
class Aboutus extends CI_Controller{
    function __construct(){
        parent::__construct();
    }

    function index(){
        "I'm in the about controller!";
        //$this->load->view("some_view") ;
    }
}

You can access it with http://localhost/dev/aboutus

Sign up to request clarification or add additional context in comments.

3 Comments

So, I should create a separate controller for each view(page) on my website to load them into browser? Also, I've removed the /dev/ part from the routes.php. It's just pages/view/$1 now.
It's all a matter of design philosophy, CodeIgniter does not force you to do anything - but it is typical to use one controller per page. like: about.php, faq.php, contact.php, etc. The index() function is responsible for showing the default view and the sibling functions can take care of any sort of actions you wish to perform on the respective pages. I strongly advise you to read everything under Introduction, Tutorial, and General Topics.
Hmm, very interesting. I guess I'll rewrite my website slightly since what you said makes total sense to me now. Also, the routing works now that I've removed /dev/ from $route['dev/(:any)'] part too, which is what everyone here must have been trying to tell me the whole time. Thanks.
0

In your config.php leave it blank if you haven't $config['index.php'] = '';

3 Comments

The $config['index_page']? Yeah it's blank.
try this to htaccess: RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt) RewriteRule ^(.*)$ /index.php/$1 [L]
Didn't work. Instead of Code Igniter's 404 I get Not Found page.

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.