【问题标题】:404 when accessing Codeigniter page访问 Codeigniter 页面时出现 404
【发布时间】:2017-02-21 21:53:57
【问题描述】:

我遇到了与CodeIgniter: 404 Page Not Found on Live Server 相同的问题。在应用程序/控制器中,我有 Welcome.php,如下所示:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
 *      http://example.com/index.php/welcome
 *  - or -
 *      http://example.com/index.php/welcome/index
 *  - or -
 * Since this controller is set as the default controller in
 * config/routes.php, it's displayed at http://example.com/
 *
 * So any other public methods not prefixed with an underscore will
 * map to /index.php/welcome/<method_name>
 * @see https://codeigniter.com/user_guide/general/urls.html
 */
public function index()
{
    $this->load->view('welcome_message');
}

但是,当我访问 mysite.com、mysite.com/welcome、mysite.com/Welcome 和 mysite.com/Welcome.php 时,我每次都会收到 404。

我的路线.php:

$route['default_controller'] = "home";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

我有一个文件,Home.php:

<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

  public function __construct() {
    parent::__construct();
    $this->load->model('general_model');
  }

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

  /*
   public function coming(){
   $this->load->view("home/coming_soon_view");
   }
  */
 }

我的 .htaccess:

RewriteEngine on
   RewriteCond $1 !^(phpMyAdmin|index\.php|robots\.txt)
   RewriteRule ^(.*)$ index.php?/$1 [L]

<IfModule authz_core_module>
    Require all denied
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>

我真的很难看出我做错了什么。

【问题讨论】:

  • 你的config['index'] var 设置为什么?
  • 来自 config.php?
  • 是的,$config['index_page']
  • 是否启用了 mod_rewrite?
  • @qwertzman 请问我怎么知道?

标签: php codeigniter


【解决方案1】:

我在共享主机上有过这种经验,然后以某种方式在互联网上找到但忘记了谁给出了这个例子,也许你可以试试。

<IfModule mod_rewrite.c>
  RewriteEngine On
  # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
  #  slashes.
  # If your page resides at
  #  http://www.example.com/mypage/test1
  # then use
  # RewriteBase /mypage/test1/
  RewriteBase /
  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>

【讨论】:

    【解决方案2】:

    当您将 base_url 设置为时

    $protocol = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) ? 'https://' : 'http://';
    $config['base_url'] = $protocol . $_SERVER['HTTP_HOST'] . str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);
    

    那么这就是您在 .htaccess 文件中所需要的全部内容

    RewriteEngine on
    # → Internally route to /index.php
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
    

    您可以稍后对其进行扩展,现在只需使用最少的设置。

    【讨论】:

    • 我试过了,只要我有 $route['default_controller'] = "welcome";但不是 $route['default_controller'] = "home";
    • 我现在已经设置好了,但是每当我去 tripmatcher.herokuapp.com/login 时,它只会显示 404,尽管我的控制器中有一个 login.php。
    【解决方案3】:
    <IfModule mod_rewrite.c>
            RewriteEngine on
            RewriteBase /
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ /index.php/$1 [L]
    </IfModule>
    

    这对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-15
      • 1970-01-01
      • 2014-10-10
      • 2018-01-19
      • 1970-01-01
      • 2023-03-11
      • 2014-07-06
      • 1970-01-01
      相关资源
      最近更新 更多