【问题标题】:URL Not Found (CodeIgniter App on Amazon EC2)未找到 URL(Amazon EC2 上的 CodeIgniter 应用程序)
【发布时间】:2023-03-25 19:18:01
【问题描述】:

填写并提交此表格后:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>My App</title>   
</head>
<body>
    <?php echo validation_errors(); ?>
    <?php echo form_open('search'); ?>
        <p>
            <input type="input" name="username" placeholder="@UserName" />
            <input type="submit" name="submit" value="Search" />
        </p>        
    </form>    
</body>
</html>

我提交的数据首先被处理,用户应该被重定向到search_page3.php。相反,我收到了这个错误:

找不到

在此服务器上未找到请求的 URL /search。

Apache/2.2.24 (Amazon) 服务器位于 ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com 端口 80

这是我的控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Search extends CI_Controller {
    public function index()
    {           
        $this->load->helper('form');
        $this->load->library('form_validation');

        // ...

        $this->form_validation->set_rules('username', 'username', 'required');
        $username = $this->input->post('username'); 

        if($this->form_validation->run() === FALSE || $this->val_username($username) === FALSE)
        {
            $this->load->view('search_page2');
        }       
        else
        {   
                    // process this data            
            // ...

            $data['data'] = $data;      
            $this->load->view('search_page3', $data);                       
        }               
    }   
    // additional helper functions 
    // ...
}
/* End of file search.php */
/* Location: ./application/controllers/search.php */

我在this answer 中读到,Juan Carlos 通过将 public_html 的 AllowOverride 设置为 All 解决了他的 URL 问题。 为了安全起见,我的问题是,我该如何解决我的错误?

这是我的 CodeIgniter application/config/config.php 文件中的基本网址:

$config['base_url'] = 'http://xxx-xx-xxx-xxx-xxx.compute-1.amazonaws.com/';

这是我的 EC2 实例中的httpd.conf

# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories). 
#
# First, we configure the "default" to be a very restrictive set of 
# features.  
#
<Directory />
    Options FollowSymLinks
    AllowOverride None  
</Directory>

#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#

#

这是我的.htaccess 文件,位于/var/www/html

RewriteEngine On

# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is www.example.com/site_folder/, use /site_folder/

RewriteBase /

# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# For reuests that are not actual files or directories,
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]

【问题讨论】:

标签: php codeigniter amazon-ec2


【解决方案1】:

http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride

如果将AllowOverride 设置为None,则.htaccess 文件将被完全忽略。因此,index.php 不会被重写。尝试将form_open 设置为form_open('index.php/search'); 以确认。

编辑httpd.conf并将AllowOverride None更改为AllowOverride All并重启httpd。

另外,仅供参考,您并没有重定向到search_page3.php,而是根据一组条件加载特定视图。当你说“重定向”时,我会寻找redirect('somepage');

【讨论】:

  • +1 嗨风暴排水。感谢您的回复。我将 form_open('search'); 更改为 form_open('index.php/search'); 并且我的视图 search_page3.php 确实加载了(但我在 search_page3.php 上遇到了几个 PHP 未定义属性错误,但至少它已加载)!但是,当我恢复到 form_open('search'); and edited httpd.conf to AllowOverride All 并重新启动 httpd httpd -k restart 并重新加载页面时,现在我得到一个空白页面。当我再次恢复到 `form_open('index.php/search') 时,查看 search_page3.php 加载错误。
  • 顺便说一句,昨天我将权限设置为chmod 777 /var/www/html -R
  • chmod 0755 可能是一个更理智的设置(777 提供执行权限,这并不是您在文档根目录中真正想要的东西)。您在根 html 文件夹中工作对吗?即你不在 /var/www/html/codeigniter 但在 /var/www/html
  • +1 是的,我在/var/www/html/ 工作,这是一种不好的做法吗?
  • 不,标准程序位于 html 根目录中。但它对 htaccess (RewriteBase) 有所不同。是的,WAMP 的东西应该移植好的,只是对齐一切的问题。另外,将 PT 从 htaccess 中取出;应该只是索引 php 行上的 [L]
猜你喜欢
  • 1970-01-01
  • 2015-11-14
  • 2011-05-25
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 2013-06-01
  • 1970-01-01
相关资源
最近更新 更多