【问题标题】:routing/rewrites/removing index.php: Only works for default_controller, but not for the rest of the views路由/重写/删除 index.php:仅适用于 default_controller,但不适用于其余视图
【发布时间】:2012-09-23 16:10:04
【问题描述】:

按照教程从 Code Igniter 中的 URL 中删除 index.php

它现在适用于我的default_controller,但不适用于其他页面(或视图)。

我有一个控制器,Pages,它有一个方法,View($page = 'home'),根据传递的参数加载其他内容的页面。

如果我在 URL 中输入localhost/dev - 我会登陆我的主页,这是正确的。

如果我输入 localhost/dev/aboutus - 我会收到 404。只有当我输入localhost/dev/pages/view/aboutus 时它才有效。

我希望通过输入localhost/dev/aboutus 来显示AboutUs 视图。

Routes.php

$route['default_controller'] = "pages/view";
$route['dev/(:any)'] = "dev/pages/view/$1";
$route['404_override'] = '';

Pages.php (控制器)

<?php
    class Pages extends CI_Controller
    {
        public function view($page = 'home')
        {
            if (!file_exists('application/views/pages/' . $page . '.php'))
            {
                // Whoops, we don't have a page for that!
                show_404();
            }

            $data['title'] = ucfirst($page); // Capitalize the first letter

            $this->load->view('templates/header', $data);
            $this->load->view('pages/' . $page);
            $this->load->view('templates/footer');
        }
    }
?>

.htaccess 文件 (位于 /dev/ 文件夹中)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /dev/

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule> 

【问题讨论】:

  • 您的 htaccess 重写 /dev/ 路径,但 routes.php 指向名为 dev 的控制器 - 您的控制器是页面而不是 dev
  • 您确定启用了 mod_rewrite 吗?你的服务器是什么操作系统?
  • @jtheman 删除了/dev/ 部分。还是不行。
  • @JordanArseno 我现在正在从 Wamp 运行它。稍后我会在线上传到我的 Linux 主机。
  • 您是从右侧删除它还是两者都删除?这个问题的主题也是错误的,因为这个问题与 index.php 无关,但与路由/重写有关。

标签: php .htaccess codeigniter


【解决方案1】:

这条路线 $route['dev/(:any)'] = "dev/pages/view/$1"; 有点可疑,因为 CodeIgniter 路线不应包含项目名称(在您的情况下为 dev)。它们从控制器级别开始,而不是项目级别。

您编写的路由意味着如果用户键入 http://localhost/dev/dev/something(是的,两个开发人员),他们将被路由到控制器 dev.php 并进入带有原型的函数:function('view', 'something'){}

为什么这么复杂?您应该将所有主页名称都放入控制器中。只有在特殊情况下才应该触及路线。

application/controllers 中创建一个文件aboutus.php,内容如下:

<?php
class Aboutus extends CI_Controller{
    function __construct(){
        parent::__construct();
    }

    function index(){
        "I'm in the about controller!";
        //$this->load->view("some_view") ;
    }
}

您可以通过http://localhost/dev/aboutus访问它

【讨论】:

  • 那么,我应该为我网站上的每个视图(页面)创建一个单独的控制器以将它们加载到浏览器中吗?另外,我已经从routes.php 中删除了/dev/ 部分。现在只是pages/view/$1
  • 这完全是设计理念的问题,CodeIgniter 不会强迫你做任何事情——但通常每页使用一个控制器。例如:about.phpfaq.phpcontact.php 等。index() 函数负责显示默认视图,兄弟函数可以处理您希望在相应页面上执行的任何类型的操作。我强烈建议您read 简介、教程和一般主题下的所有内容。
  • 嗯,很有趣。我想我会稍微重写我的网站,因为你所说的现在对我来说完全有意义。此外,由于我也从$route['dev/(:any)'] 部分中删除了/dev/,因此路由工作正常,这也是这里的每个人一直试图告诉我的。谢谢。
【解决方案2】:

如果你还没有,请在你的 config.php 中留空 $config['index.php'] = '';

【讨论】:

  • $config['index_page']?是的,它是空白的。
  • 试试这个到 htaccess: RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt) RewriteRule ^(.*)$ /index.php/$1 [L]跨度>
  • 没用。而不是 Code Igniter 的 404 我得到 Not Found 页面。
猜你喜欢
  • 2013-12-14
  • 1970-01-01
  • 2011-02-14
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
  • 2020-06-05
  • 2014-07-05
相关资源
最近更新 更多