【问题标题】:Codeigniter - removing index.php causes links to breakCodeigniter - 删除 index.php 会导致链接中断
【发布时间】:2016-11-08 15:25:58
【问题描述】:

我在 URL 中遇到 index.php 的这个问题,当它删除时,指向我的控制器的链接不再工作?

我修改了以下内容:

config.php:

$config['index_page'] = '';

URI 协议原样:

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

.htaccess:

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

我还通过phpinfo(); 验证了模块mod_rewrite 已加载。

当前网址采用以下格式: http://localhost:100/ampp/

主页上的登录表单:

<form class="form-signin" method="POST" action="<?php echo site_url('Auth/login') ?>">
    <span id="reauth-email" class="reauth-email"></span>

    <input type="email" id="inputEmail" class="form-control" placeholder="Email" name="email" required autofocus>
    <input type="password" id="inputPassword" class="form-control" placeholder="Password" name="password" required>
    <br>
    <button type="submit" name="submit" value="submit" class="btn btn-lg btn-primary btn-block btn-signin">Sign in</button>
</form>

但是,当我提交表单时,我得到 404

Not Found

The requested URL /ampp/Auth/login was not found on this server.

我已检查日志文件以查看问题所在,但日志文件仅表明未找到 Auth。

File does not exist: /var/www/html/ampp/Auth, referer: http://localhost:100/ampp/

从技术上讲,该文件不在日志文件声明的目录中(这是真的,因为该文件位于 /ampp/application/controller/ 中),但我认为 Codeigniter 会适当地解决这个问题。

树形结构

   .
├── cache
│   └── index.html
├── config
│   ├── autoload.php
│   ├── config.php
│   ├── constants.php
│   ├── database.php
│   ├── doctypes.php
│   ├── foreign_chars.php
│   ├── hooks.php
│   ├── index.html
│   ├── memcached.php
│   ├── migration.php
│   ├── mimes.php
│   ├── profiler.php
│   ├── routes.php
│   ├── smileys.php
│   └── user_agents.php
├── controllers
│   ├── Auth.php
│   ├── Home.php
│   ├── index.html
│   ├── Login.php
│   └── Main.php
├── core
│   └── index.html
├── helpers
│   └── index.html
├── hooks
│   └── index.html
├── index.html
├── language
│   ├── english
│   │   └── index.html
│   └── index.html
├── libraries
│   └── index.html
├── logs
│   └── index.html
├── models
│   ├── index.html
│   ├── UserAuth.php
│   └── User.php
├── third_party
│   └── index.html
└── views
    ├── errors
    │   ├── cli
    │   │   ├── error_404.php
    │   │   ├── error_db.php
    │   │   ├── error_exception.php
    │   │   ├── error_general.php
    │   │   ├── error_php.php
    │   │   └── index.html
    │   ├── html
    │   │   ├── error_404.php
    │   │   ├── error_db.php
    │   │   ├── error_exception.php
    │   │   ├── error_general.php
    │   │   ├── error_php.php
    │   │   └── index.html
    │   └── index.html
    ├── home.php
    ├── index.html
    ├── login.php
    └── report.php

Auth.php 控制器

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

class Auth extends CI_Controller {

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

                $this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
                $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
                $this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
                $this->output->set_header('Pragma: no-cache');
        }
        public function index() {
                $session = $this->session->userdata('authUser');
                if(!$session) {
                        redirect('login','refresh');
                } else {
                        $this->propagate($session['id']);
                }
        }
        public function login() {
                $this->verifylogin();
        }
        public function verifylogin() {
                $email = $this->input->post('email');
                $password = $this->input->post('password');

                $data = array('email'=>$email, 'password'=>$password);
                $validation = $this->UserAuth->validateUser($data);

                if($validation == false) {
                        //show login with erro message - no error message implemented
                        $this->load->view('login');
                } else {
                        $result = $validation;
                        $sess_array = array();
                        foreach($result as $row) {
                                $sess_array = array(
                                        'authID' => sha1($result[0]->id),
                                        'id' => $result[0]->id,
                                        'email' => $result[0]->email
                                        );
                                $this->session->set_userdata('authUser', $sess_array);
                        }
                        redirect('home','refresh');
                }
        }
        public function logout() {
                $this->session->unset_userdata('authUser');
                $this->session->sess_destroy();
                redirect('Main', 'refresh');
        }
}

.htaccess 检查

我想我试一试,看起来这可能是实际 .htaccess 文件的问题。为什么?

我已经用这个替换了.htaccess 文件的内容:

order deny,allow
deny from all

但我仍然可以查看主登录页面(尽管 .htaccess 不应该允许我)

更新: 好的,因为我注意到任何 .htaccess 文件都不起作用,所以我开始挖掘以找出它不起作用的原因。

因此问题出在虚拟主机配置上...

<Directory "/"> //<-------- This was the issue.
    AllowOverride All
    Order Allow,Deny
    Allow from all
</Directory>


<Directory "/var/www/html/ampp"> //<------- This has resolved then issue.
    AllowOverride All
    Order Allow,Deny
    Allow from all
</Directory>

【问题讨论】:

  • 尝试删除RewriteBase并将这一行RewriteRule ^(.*)$ index.php/$1 [L]更改为RewriteRule ^(.*)$ /ampp/index.php/$1 [L],这是我的设置。
  • @Blinkydamo 不-仍然是同样的问题
  • 请记住,您还应该在 codeigniter 中设置 base_url
  • @killstreet base_url 已设置

标签: php apache .htaccess codeigniter mod-rewrite


【解决方案1】:

我假设您的 Auth 是一个控制器类,而 login 是该类的一个方法。只需检查类文件的文件名,即检查 Auth 控制器的文件名,它应该是第一个字符大写,“Auth.php”。然后将表单的操作更改为

site_url('auth/login')

注意:在 url 中使用小写的身份验证。

希望它能解决你的问题。

【讨论】:

  • 还可以尝试在您的 htaccess 中的 index.php 前面添加斜杠“/”
  • RewriteRule ^(.*)$ /index.php/$1 [L]
  • 我认为小写不会是问题
  • 它可能是......因为最近版本的 codeigniter 是区分大小写的 unix/linux 系统。
  • @puncoz 没有帮助 - 同样的问题
【解决方案2】:

把你的.htaccess改成这个

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

在配置中

$config['base_url'] = 'http://localhost:100/ampp/';

在表格中

<form class="form-signin" method="POST" action="<?php echo base_url();?>auth/login">

在此之前确保有控制器调用auth 和方法调用login

【讨论】:

  • 你在哪里改变了什么??
  • 我已应用您建议的 .htaccess,但问题仍然存在。
  • 据我所知,控制器名称应该是大写的,不是吗?此外,基本 url 已设置为该值
  • 文件名应为Auth.php,类名也应相同
  • 所以文件名和类名已经是大写,基本 url 已经设置 - 我已经根据你的建议修改了表单操作,但无济于事 - 同样的问题
【解决方案3】:

事实证明,问题不在于 .htaccess 它本身,也不是 Codeigniter,而是 Apache 配置。

<Directory "/"> //<-------- This was the issue.
    AllowOverride All
    Order Allow,Deny
    Allow from all
</Directory>


<Directory "/var/www/html/ampp"> //<------- This has resolved then issue.
    AllowOverride All
    Order Allow,Deny
    Allow from all
</Directory>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多