404访问Codeigniter页面时

时间:2017-02-21 21:53:58

标签: php codeigniter

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

valueof(intvariable)

然而,当我去mysite.com,mysite.com / welcome,mysite.com / welcome和mysite.com/Welcome.php时,我每次都会得到404.

我的routes.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');
}

我有一个文件,Home.php:

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

我的.htaccess:

<?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");
   }
  */
 }

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

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

将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]

您可以稍后展开,只需立即使用最小设置。

答案 2 :(得分:0)

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

这对我有用。