【问题标题】:routing requests with htaccess and codeigniter使用 htaccess 和 codeigniter 路由请求
【发布时间】:2013-05-02 11:06:48
【问题描述】:

我有一个带有一个控制器的 codeigniter 应用程序 (main.php)

目前,我已将 htaccess 文件设置为删除 index.php 和 main.php

所以不是 www.domain.com/index.php/main/function_name,而是 www.domain.com/function_name

我的 htaccess 文件如下所示:

 <IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /


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?/main/$1 [L]

ErrorDocument 404 /index.php

我现在需要向网站添加一大块。它目前内置在一个单独的 codeigniter 应用程序中,我需要将它移过来。该站点的新部分位于 controllers/manage_emails/contacts.php...

我的问题是,在大多数情况下,我如何更改 htaccess 文件以从 URL 中删除 main.php,但是如果您输入 www.domain.com/manage_emails/contollername,它将转到正确的位置控制器。

谢谢!

【问题讨论】:

    标签: php apache .htaccess codeigniter


    【解决方案1】:

    据我了解,您有两个不同的问题:

    1.从Codeigniter的默认中删除index.php文件

    2.将任何调用从“www.domain.com/main/method”重定向到“www.domain.com/method”

    对于第一个问题,我总是使用 Elliot Haughin 的 htaccess 文件进行 CI:CodeIgniter and mod_rewrite with multiple applications

    对于你的第二个问题,你需要在 CI 中更改 config/routes.php,并添加这一行:

    $route['(:any)'] = "main/$1"; 
    

    ..你也可以查看这个帖子:Codeigniter, bypassing the main or default controller

    尝试将这两个问题分开,就像有人说的那样,坚持使用 CI 进行路由,只使用 htaccess 从 URL 中删除 index.php。

    【讨论】:

    • 真的是 3 个问题...因为我还需要访问 controllers/manage_emails/something.php 文件夹中的控制器
    • 你检查第二个链接了吗?这也涵盖了您的第三个问题。
    【解决方案2】:

    如果您从 .htaccess 文件中删除任何 URI 路由,并让您的 CodeIgniter 的路由为您执行此操作,您的应用程序将更容易管理。

    您的 .htaccess 应该只有标准的“摆脱 index.php”代码(最后一部分,其中没有 /main)。然后,您的应用程序的路由可以定义其余 URL 的时间/位置。

    仅供参考,如果您使用的是更新版本的 CI,例如 2.1.x,则不需要 .htaccess 中的系统和应用程序文件夹特定规则。

    【讨论】:

    • 谢谢,那么我的路线是将所有内容重定向到 main.php,除了 /manage_emails/controllername
    • 阅读文档,路由功能没有改变。这很简单,你应该只需要两条路线。
    【解决方案3】:

    除了@despina 的回答,我还想补充一点:

    你必须在最后一个路由设置$route['(:any)'] = "main/$1";,因为CI会按照它们在你的routes.php文件中出现的顺序处理这些路由,所以如果你把$route['(:any)']放在顶部,它会处理任何事情。

    所以你的 route.php 文件会是这样的:

    $route['controllers/manage_emails/something.php'] = 'controllers/manage_emails/something.php'
    $route['(:any)'] = "main/$1"; 
    

    【讨论】:

      【解决方案4】:

      首先,您的 2 条规则不正确,很可能无法正常工作。这是您的 .htaccess 的固定版本,添加了一条要求的新规则:

      RewriteEngine On
      RewriteBase /
      
      RewriteRule ^(?:application|system)(/.*|)$ /index.php?/$1 [L,NC]
      
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} !^/manage_emails/contollername [NC]
      RewriteRule ^(.*)$ index.php?/main/$1 [L]
      

      错误文档 404 /index.php

      【讨论】:

      • 谢谢,这似乎不起作用......不确定我的问题是否清楚,但 manage_emails 是控制器文件夹中的子文件夹......然后在那个 manage_emails 文件夹中我有一堆我想访问的各种控制器。
      • @Bill:当您访问manage_emails folder 内的任何控制器时,这些 URL 究竟是什么?我也对您的问题RewriteRule ^(.*)$ index.php?/main/$1 [L] 这一行中的? 感到困惑。你确定?index.php 之后是必需的吗?
      • 所以 URL 将是:domain.com/welcome 将映射到 controllers->main.php/welcome 该站点的所有功能都在 main.php 中。我需要在 controllers->manage_emails->controllername.php/somefunction 中添加一个单独的部分,我希望一切都按现在的方式工作,除了能够访问 manage_emails 文件夹中的控制器。我不确定是什么?在我拥有的 htaccess 文件中,但 htaccess 文件可以从 URL 中删除 index.php 和 main.php。所以我现在只需要添加其他的东西。
      【解决方案5】:
      //config/routes.php
          $route['404_override'] = 'main/route';
      //controllers/main.php
      class Main {
      ...........
         function route() {
            $methodName = $this->uri->segment(1);
            if(method_exists($methodName, $this)) {
             $this->{$methodName}();
            } else {
              show_404();
            }
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-04
        • 1970-01-01
        • 2019-07-30
        • 2013-06-19
        • 2017-01-08
        • 2011-12-04
        • 2011-06-18
        • 2016-11-05
        相关资源
        最近更新 更多