0

I am in the threshold of exploring the CodeIgniter Framework, and I am facing a very basic problem here. I have a controller named pages, and it has a function called view.

Problem 1: Now when I type example.com/myfolder/ I do land to my page. But when I try something like example.com/myfolder/index.php/pages/view or example.com/myfolder/pages/view, it gives me a 404 Error.

Problem 2: I need to remove the index.php from the URL. Please rectify the .htaccess pasted below.

Controller

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);
    }

}

Routes

$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

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

Update

This might be important. I am running my site on a shared windows hosting, which is using IIS. So I suppose my .htaccess needs to be converted to web.config.

UPDATE WEBCONFIG ADDED

Now, I have added the web.config from the link provided in one the answers. Thanks a lot; it was really helpful, but still I am getting the same old 404 Error. I tried changing the following my configuration file

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

But now I am getting errors about

<!doctyle HTML>

How can I fix this?

4

3 回答 3

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'] = '';
于 2013-05-28T06:40:36.693 回答
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> 

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

改变:

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

至:

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

否则你可能会得到一个404 No input specified错误

于 2013-05-28T06:51:04.020 回答
0

我猜这条线是为 404 服务的:

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

尝试将其替换为:

if (!file_exists(APPPATH.'views/pages/' . $page . '.php')) {
于 2013-05-28T08:13:21.057 回答