【问题标题】:can CodeIgniter display 404 page for mistyped index.php url?CodeIgniter 可以为输入错误的 index.php url 显示 404 页面吗?
【发布时间】:2015-06-11 05:41:25
【问题描述】:

我在 apache php 上使用 CodeIgniter,如果我在控制器级别或方法级别输入错误的 url(例如 http://www.example.com/index.php/mistypedcontroller/mistypedfunction),我会得到 404 页面。

但是如果我输入http://www.example.com/mistyped.php/controller/function,我会得到一个空白页。有没有办法在这里显示相同的 404 页面?

【问题讨论】:

    标签: codeigniter http-status-code-404


    【解决方案1】:

    "CodeIgniter 是否可以显示 404 页面错误的 index.php url?"

    不,它不能。如果您输入错误index.php,那么您将不再使用/使用 CodeIgniter。

    查看这个文件,你会看到index.php 正在构建所有内容,最后一行包含了 CodeIgniter.php 核心文件。


    www.example.com/index.php/mistypedcontroller/mistypedfunction
    

    上面的第一个是使用index.php,所以它是由CodeIgniter生成的。

    www.example.com/mistyped.php/controller/function
    

    第二个,因为它没有使用index.php,与 CodeIgniter 完全无关。

    有没有办法在这里显示相同的 404 页面?

    它必须是一个独立文件,并通过 Apache 的 .htaccess 文件使用 ErrorDocument 404 选项为您的特定服务器配置。

    或者,如果你只是关注the documentation for removing the index.php from the URL,就不会出现上述情况;您损坏的 URL 示例将导致 CodeIgniter 404 错误,因为任何/每个 URL(指向不存在的文件)都被强制放入您的 index.php 文件中。

    如果您的 Apache 服务器启用了 mod_rewrite,您可以使用带有一些简单规则的 .htaccess 文件轻松删除此文件。以下是此类文件的示例,使用“否定”方法,其中除了指定项目之外的所有内容都被重定向:

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

    在上面的示例中,任何 HTTP 请求,除了现有目录和现有文件都被视为对您的 index.php 文件的请求。

    【讨论】:

      猜你喜欢
      • 2012-08-07
      • 2012-07-12
      • 2013-08-13
      • 2012-07-17
      • 2014-08-01
      • 2012-12-21
      • 2014-05-25
      • 2017-07-02
      • 1970-01-01
      相关资源
      最近更新 更多