CodeIgniter URL - 404错误

时间:2013-05-28 06:35:54

标签: php codeigniter frameworks

我正处于探索CodeIgniter框架的门槛,我在这里遇到了一个非常基本的问题。我有一个名为pages的控制器,它有一个名为view的函数。

问题1: 现在当我输入example.com/myfolder/时,我会登陆我的页面。但是当我尝试像example.com/myfolder/index.php/pages/view或example.com/myfolder/pages/view这样的内容时,它会给我一个404错误。

问题2: 我需要从URL中删除index.php。请纠正下面粘贴的.htaccess。

控制器

class Pages extends CI_Controller {

    public function index() {
        $this->view("home");
    }

    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
        $data['company_name'] = "Demo Company 2013";
        $this->load->view('templates/header', $data);
        $this->load->view('pages/' . $page, $data);
        $this->load->view('templates/footer', $data);
    }

}

路线

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

的.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /myfolder/
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?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

配置

$config['index_page'] = '';
$config['base_url'] = 'http://example.com/myfolder/';

更新

这可能很重要。我在共享的Windows主机上运行我的网站,该主机正在使用IIS。所以我想我的.htaccess需要转换为web.config

UPDATE WEBCONFIG ADDED

现在,我已经从答案中提供的链接添加了web.config。非常感谢;它真的很有帮助,但我仍然得到相同的旧404错误。我尝试更改以下配置文件

 $config['uri_protocol'] = 'QUERY_STRING';

但现在我收到了关于

的错误
<!doctyle HTML>

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

您可以像这样编写.htacces

Options -Indexes

Options +FollowSymLinks

DirectoryIndex index.php

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !index.php
    RewriteRule (.*)\.php$ index.php/$1
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [QSA,L]

</IfModule>

然后检查你的config.php文件并进行这样的更改。

$config['base_url'] = '';

$config['index_page'] = '';

答案 1 :(得分:0)

这是我通常使用的,适用于多种服务器:

DirectoryIndex index.php

<IfModule mod_rewrite.c>

  RewriteEngine on
  RewriteBase /myfolder
  RewriteCond $1 !^(index\.php|user_guide|robots\.txt)
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d 
  RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

</IfModule>

<IfModule !mod_rewrite.c>
  ErrorDocument 404 index.php
</IfModule> 

根据您的服务器配置,您可能需要在?后删除index.php

变化:

RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

要:

RewriteRule ^(.*)$ index.php/$1 [L,QSA]

否则您可能会收到404 No input specified错误

答案 2 :(得分:0)

我猜这条线服务于404:

if (!file_exists('application/views/pages/' . $page . '.php')) {

尝试将其替换为:

if (!file_exists(APPPATH.'views/pages/' . $page . '.php')) {
相关问题