14

For technical reasons, I don't want to use .htaccess to remove index.php from URL in CodeIgniter. I have seen the route section. I also tried with a route like this:

$route['(:any)'] = 'index.php/$1';

But it is not working.

Is it possible to remove index.php using the router in CodeIgniter?

4

2 回答 2

9
Is it possible to remove index.php without using .htaccess?

No. it is something beyond php or codeigniter, its a server thing.

Why ?

What happens when user try to go to localhost/codeigniter/welcome/index ?

MVC basically hide all your php files inside its folders. and present only one way for any user to interact with your code through index.php

what happen is

  1. HTTP request to your website directed to index.php
  2. index.php load codeigniter bootstrap and call codeigniter router
  3. router look up the url typed (welcome/index) then go fetch the file welcome from inside controller folder and call index function.

this is plain simple what all MVC frameworks do, hide your code and use a single front file to handle all requests and redirect them using a router class.

so if you want to hide index.php then you need to find a way to tell your server to simply redirect any call to index.php.

index.php is the access door for your website, and your server need to know that all requests must be sent to it. some way or another, and thats why index.php/ must exisit in ur url or use some server configuration to redirect calls to it. but it is always loaded

于 2014-03-12T18:23:30.477 回答
1

我已经完成了以下操作来处理不需要index.php在您的 url 中包含的路由。

  1. 出于安全原因,index.php我已重命名。index_ci.php
  2. .htaccess文件中,我有以下内容:

    DirectoryIndex index_ci.php index.html
    RewriteCond %{REQUEST_FILENAME} !-f  
    RewriteCond %{REQUEST_FILENAME} !-d  
    RewriteCond $1 !^(index_ci\.php) # do not allow direct access  
    RewriteRule ^(.*)$ index_ci.php/$1 [L,QSA]
    
  3. routes.php现在接受不带index.php前缀的网址。例子:

    $route['welcome'] = "welcome";
    
于 2013-06-24T08:03:59.557 回答