简体   繁体   中英

routing/rewrites/removing index.php: Only works for default_controller, but not for the rest of the views

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> 

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

如果没有$ config ['index.php'] ='',请在config.php中将其留空。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM