【问题标题】:URI Routing In Codigniter Without Htaccess没有 Htaccess 的 Codeigniter 中的 URI 路由
【发布时间】:2012-01-10 10:50:48
【问题描述】:

这是我的网址:

xxx.myserver.net

在这个站点中,我想路由控制器,这意味着当我链接这种类型的 url 时:

xxx.myserver.net/aaaaa

我想将它重定向到我想要的控制器中,这意味着我想重定向到:

xxx.myserver.net/index.php/controller-name/funation-name/aaaaa

我想在不使用 htaccess 方法的情况下仅通过使用 codeigniter uri 路由方法来完成此操作。

当我尝试这样做时,我收到一条错误消息:

Not Found

The requested URL  not found on this server.

这是我在 routes.php 页面中给出的代码:

$route['/(:any)'] = "xxx.myserver.net/index.php/controller-name/funation-name/$1";

我的代码有什么问题?

我该怎么做?

【问题讨论】:

  • 如果没有.htaccess,您将无法执行此操作,您会将路由配置文件与.htaccess 所做的混淆。路由配置文件只路由到控制器,而不是服务器上的实际文件(这是 htaccess 正在做的)。
  • 您的意思是路由配置文件仅用于路由控制器而不用于服务器路径?
  • 正确,您只能将参数路由到控制器,而不是文件或任何类似的东西。

标签: php uri redirect codeigniter-2


【解决方案1】:

如果不使用.htaccess 文件,您将无法做到这一点。从高层次来看,.htaccess 文件的目的是通过 CodeIgniter 前端控制器将所有请求路由到您的域,即根目录中的index.php 文件。这是documentation,它解释了如何创建.htaccess 文件并从URL 中删除index.php。换句话说,它说明了如何将任意请求视为对index.php 文件的请求。

当您向xxx.myserver.net/aaaaa 发出请求时,Web 服务器正在您的根目录中搜索资源aaaaa。 CodeIgniter 甚至没有处理该请求。由于您没有标题为 aaaaa 的资源,因此您收到错误消息。

要在没有 .htaccess 文件的情况下使用 CodeIgniter,您需要通过 index.php 传递每个请求。例如:

xxx.myserver.net/index.php/aaaaa

然后你可以像这样创建你的路线:

$route['(:any)'] = 'controller_name/function_name/$1';

【讨论】:

  • 我这样设置 $route['(:any)'] = 'controller_name/function_name/$1';并采取以上得到这个错误 404 Page Not Found The page you request was not found.
猜你喜欢
  • 2013-06-19
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
  • 2014-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多