Codeigniter url does not work on the web server

时间:2017-05-16 09:24:14

标签: php codeigniter

I was developing in XAMPP environment on my local machine, and everything is fine. I can access my controller like this:

localhost/myproject/index.php/catalog/home/get

catalog is the sub folder under controllers.

However, after I uploaded my project to the server. I should be able to access my page by the following url:

http://www.example.com/index.php/catalog/home/index

but this gives me a 404 page.

The only url works is the default page I defined in the route.php file:

$route['default_controller'] = 'welcome';

In the config.php, I set my base_url to be http://www.example.com/

All my controllers' first letter are capitalized and I'm using CI 3.1X. My server is a linux which uses nginx instead of apache. I tried to modify or delete my .htaccess file, but it doesn't solve my problem.

I'm not looking for an exact answer, I would like to know how to debug my code in this situation.

Here is an example of one of my controllers:

<?php

class Survey extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('catalog/model_survey');
        $this->load->helper('url');
    }

    public function index()
    {
        $error = $this->input->get('error');

        if (isset($error)) {
            $data['error'] = true;
        } else {
            $data['error'] = false;
        }

        $data['action'] = "index.php/catalog/success";
        $data['base'] = base_url();

        $this->load->view('catalog/survey', $data);
    }
}

Here is my folder structure:

application
|-- cache
|-- config
|-- controllers
|   |-- admin
|   |   |-- common
|   |   |-- redpacket
|   |   |-- setting
|   |   `-- tools
|   `-- catalog
|-- core
|-- helpers
|-- hooks
|-- language
|   |-- chinese
|   |   `-- admin
|   |       |-- common
|   |       |-- redpacket
|   |       |-- setting
|   |       `-- tools
|   `-- english
|       `-- admin
|           `-- common
|-- libraries
|-- logs
|-- models
|   |-- admin
|   |   |-- common
|   |   |-- redpacket
|   |   |-- setting
|   |   |-- tools
|   |   `-- user
|   `-- catalog
|-- third_party
`-- views
|-- admin
    |   |-- common
    |   |-- redpacket
    |   |-- setting
    |   `-- tools
    |-- catalog
    `-- errors
|-- cli
        `-- html

5 个答案:

答案 0 :(得分:2)

it looks like you forgot to update .htaccess file on the server

答案 1 :(得分:2)

请考虑以下清单。

  1. 检查控制器文件名Survey.phpS必须为大写。
  2. 检查文件夹名称catalog是否区分大小写。
  3. 视图和模型相同。
  4. 我在大多数项目中都使用了以下.htaccess代码。

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

    编辑回答:

    在您指定的目录层次结构中,您的所有controllermodel名称均为小写。根据CI 3.X标准,Classes第一个字符大写,并且适用于它们各自的文件。

答案 2 :(得分:1)

本地主机/ myproject的/ index.php的/目录/家/ GET

这里localhost是主机名, myproject是项目名称 目录是控制器子文件夹名称, 家是控制器名称, 并获得在家庭控制器下的功能。

在Home中使H资本成为类名, 并检查.htaccess文件

答案 3 :(得分:1)

在比较我的本地环境和生产环境后,我发现我在本地计算机上使用Apache,但在服务器上使用nginx。因此,我猜测问题将是我的nginx配置文件的设置。

在做了一些研究之后,这就是我在网上找到的:https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/?highlight=codeigniter

我将我的nginx配置文件修改为此,现在一切正常。通过使用此配置,它还可以删除url中的index.php。您可以按类型http://www.example.com/survey访问网络,或者如果您执行了我所做的操作,在控制器下添加子文件夹,只需在网址http://www.example.com/catalog/survey

中添加子文件夹即可

答案 4 :(得分:0)

在评论中记入kishor10d。我想强调一下这是一种可能的解决方案:

如果要在Windows计算机上进行开发并部署到linux Web服务器,则文件的大写字母至关重要。 Windows忽略大小写敏感,Linux需要匹配。对我来说,解决方法是将控制器的文件名大写。