【发布时间】: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