Codeigniter - 删除index.php导致链接中断

时间:2016-11-08 15:25:58

标签: php apache .htaccess codeigniter mod-rewrite

我在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 check

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

我已将.htaccess文件的内容替换为:

order deny,allow
deny from all

然而,我仍然能够查看主登录页面(尽管.htaccess不应该允许我这样做)

更新 好吧,因为我注意到任何.htaccess文件都无法正常工作我去挖掘找出它无法正常工作的原因。

因此问题出在vhost配置......

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

3 个答案:

答案 0 :(得分:0)

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

site_url('auth/login')

注意:在网址中使用小写的auth。

希望它能解决你的问题。

答案 1 :(得分:0)

将您的.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

  

答案 2 :(得分:0)

事实证明,问题不在于.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>