Codeigniter- how load new page after removing index.php

左心房为你撑大大i 提交于 2019-12-12 10:05:44

问题


I am new to codeIgniter. I have some links in my index file. I have removed index.php from url so now url look like

http://localhost/app/Loader/demo_page

This if my Loader code:

class Loader extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->helper('url');
    }
    public function index()
    {
        $this->load->view('header');
        $this->load->view('center');
        $this->load->view('footer');
    }
    public function demo_page()
    {
        $this->load->view('demo');
    }
}

When I click on this link, I get not found error.

Now what should I do?

I want to remove controller name also from url and want to show pretty url only like domain/app/mydemopage.php. Please help.


回答1:


You need URL Rewrite module for this to work. How to get this working depends on the web server you use.

For Apache, edit httpd.conf and uncomment (remove leading #) the following line then restart the webserver.

LoadModule rewrite_module modules/mod_rewrite.so

Add/edit .htaccess file in your CodeIgniter root folder and insert this.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

If you get RewriteEngine not allowed here error, make sure the web root folder of CodeIgniter allows FileInfo override in your webserver config httpd.conf.

AllowOverride FileInfo




回答2:


Add below lines in your .htaccess file.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

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



回答3:


You can change your route file using below example. In string I am passing controller and function (method) name. And in $route array I am passing pretty name as I like.

#Route file path : CI_project/applications/config/routes.php

$route['default_controller'] = "Dashboard";
$route['contact-us'] = "dashboard/contact_us";

But please keep in mind that if you want to make above code work you have to enable rewrite module and give this code in .htaccess.

.htaccess file code:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /prjectName/
    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>
 # If we don't have mod_rewrite installed, all 404's
  # can be sent to index.php, and everything works as normal.
  # Submitted by: Pawan Nagar
<IfModule !mod_rewrite.c>
   ErrorDocument 404 /index.php
</IfModule> 


来源:https://stackoverflow.com/questions/40734909/codeigniter-how-load-new-page-after-removing-index-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!