【问题标题】:Codeigniter The requested URL was not found on this serverCodeigniter 在此服务器上找不到请求的 URL
【发布时间】:2021-05-26 10:31:08
【问题描述】:

我正在学习 Codeigniter,但我无法解决我遇到的问题。

  • 当用户访问localhost/reservation/时,系统会显示login.php
  • 当用户点击登录按钮时,系统应该将用户重定向到另一个视图,系统将显示book.php

非常感谢一个好的解释。

查看

<form class="" method="POST"> 
   <div>
       <button class="btn btn-primary" name="login">Login  </button>
   </div>
</form> 

如您所见,我没有包含用户名和密码输入字段以缩短代码。

控制器

class Reservation extends CI_Controller {
public function _construct()
{
    parent::__construct(;
    $this->load->helper("url");
}

public function index()
{
    if(...some condition...){
        redirect('book');
    }
    $this->load->view('login');
}

public function book(){
    $this->load->view('book');
}
}

我缩短了代码,删除了不必要的代码(在我看来)以使其更短且直截了当。

路线

$route['book'] = 'reservation/book';
$route['default_controller'] = 'reservation';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

.htaccess

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

【问题讨论】:

  • 你设置了.htaccess
  • ...some condition... 应该是!is_null($this-&gt;input-&gt;post('login'))
  • @DevsiOdedra,我更新了问题以添加 .htaccess
  • @BinarWeb,我知道。问题是当redirecting occurs.
  • 将您的 .htaccess 放在应用程序文件夹之外意味着在您的目录文件夹中

标签: php codeigniter-3


【解决方案1】:

您的.htaccess 应该是这样的(删除索引页):

Options +FollowSymlinks -Indexes
RewriteEngine on

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

.htaccess 文件放到应用文件夹外的应用文件夹中

并像这样设置您的config.php

$config['base_url'] = 'http://localhost/app_folder/';
$config['index_page'] = '';

现在访问

http://localhost/app_folder/ 

将显示您的登录页面

【讨论】:

  • 添加 .htaccess ,设置你的基本 url 并访问 url ,你肯定会得到你想要的
  • 很好的解释。谢谢你。 move .htaccess to app root folder 是主要解决方案,也是`.htaccess 中的代码。
  • 我的荣幸,快乐的编码
猜你喜欢
  • 1970-01-01
  • 2017-08-07
  • 2017-06-22
  • 1970-01-01
  • 2017-03-21
  • 2010-10-16
  • 1970-01-01
  • 2018-04-19
  • 1970-01-01
相关资源
最近更新 更多