CodeIgniter:是否可以在不使用.htaccess的情况下删除index.php?

时间:2013-06-24 02:52:49

标签: codeigniter .htaccess codeigniter-routing

由于技术原因,我不想使用.htaccess从CodeIgniter中的URL中删除index.php。我看过路线部分。我也试过这样的路线:

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

但它没有用。

是否可以使用CodeIgniter中的路由器删除index.php

2 个答案:

答案 0 :(得分:9)

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

否。它是超越php或codeigniter的东西,它是服务器的东西。

为什么?

当用户尝试转到localhost/codeigniter/welcome/index时会发生什么?

MVC基本上隐藏了其文件夹中的所有php文件。并且只提供一种方式让任何用户通过index.php

与您的代码进行互动

发生了什么

  1. 指向index.php的网站的HTTP请求
  2. index.php load codeigniter bootstrap and call codeigniter router
  3. 路由器查找url类型(welcome / index)然后从控制器文件夹中获取文件welcome并调用index函数。
  4. 这很简单,所有MVC框架都会这样做,隐藏代码并使用单个前端文件来处理所有请求并使用路由器类重定向它们。

    所以如果你想要hide index.php那么你需要找到一种方法来告诉你的服务器简单地将任何调用重定向到index.php。

    index.php是您网站的访问门,您的服务器需要知道必须向其发送所有请求。某种方式,这就是为什么index.php /必须在你的url中存在或使用一些服务器配置来重定向到它的调用。但总是加载

答案 1 :(得分:1)

我已完成以下操作,以处理您的网址中不需要index.php的路由。

  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";
    
相关问题