【问题标题】:404 function is working but not showing properly404 功能正常但显示不正确
【发布时间】:2021-12-30 07:04:05
【问题描述】:

所以我有带有以下代码的 .htaccsess 文件

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine on
# Allowed the url has access to the php file without the .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php  [NC,L]
RewriteRule ^folder/?$ - [F,L]

# Force remove .php extension, if manually changed in url
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
</IfModule>

Options All -Indexes
ErrorDocument 403 /AllProject/layouts/403-page.php
ErrorDocument 404 /AllProject/layouts/404-page.php
ErrorDocument 500 /AllProject/layouts/500-page.php

所以我想为无权访问某个 url 的用户创建一个限制。当用户尝试通过url地址访问某个url时,会被重定向到404页面。

该功能有效但404页面无法正确显示,而不是使用来自.htacess的错误页面,而是来自apache的标准404错误,以下代码是

标头函数

<?php
// Check role
if ($_SESSION['role'] != 'ADMIN') {
    header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found", true, 404);
    die;
    if (!isset($_SESSION['username']) && !isset($_SESSION['role'])) {
        $containUrl = urlencode((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");

        header("Location: index?destination=" . $containUrl . "");
    }
}

然后我尝试使用 require 函数,但这也是一个问题,因为在 404-page.php 页面上我也在使用 require 函数。这使文件变得矛盾。文件代码是

404-page.php

<?php require '../koneksi/koneksi.php'; ?>
<?php require 'resources.php'; ?>

<body>

    <!-- Page Container -->
    <div class="page-container">
        <div class="error-404">
            <div class="error-bg"></div>
            <div class="error-info">
                <div class="error-text">
                    <div class="error-header">
                        <h3>404</h3>
                    </div>
                    <div class="error-body">
                        <p>Halaman yang anda cari tidak ditemukan.<br>Klik <a href="<?= $hostToRoot ?>"> disini</a> untuk kembali.
                    </div>
                    <div class="error-footer">
                        <p><?= date('Y') ?> &copy; ReD | Projects <?= $version ?></p>
                    </div>
                </div>
            </div>
        </div>
    </div><!-- /Page Container -->

    <?php require 'footer.php'; ?>

那么问题出在哪里?

【问题讨论】:

  • 为什么在您的 404 页面上使用 require 会出现问题?你的404不需要任何授权?还是只有共享代码会导致无限重定向? Including your 404 page 发送状态码时通常是正确的方法。
  • 好吧,如果我在 header 函数上使用 require,会导致像这样的错误 Warning: require(../koneksi/koneksi.php): failed to open stream: No such file or directory 文件已加载,但会变得矛盾,因为在 404-page.php 我也有一个 require 函数.
  • 那你就不能改路径吗?
  • 我不能,因为 koneksi 文件在不同的文件夹中
  • 只是不要使用相对路径作为要求,而是使用绝对路径,或者使用魔法常量之一(__DIR____FILE__)来帮助找出“在哪里”你目前是。

标签: php .htaccess http-status-code-404


【解决方案1】:

你在 .htaccess 中试试这个代码

#404 页面

ErrorDocument 404 http://localhost/not-found.php

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

所以,我只是想办法解决我的问题。 感谢@CBroe 在评论中建议我使用 DIR 它现在对我来说实际上工作得很好。

所以现在在 404-page.php 我现在正在使用

<?php require_once __DIR__ . '/../koneksi/koneksi.php' ?>

正确地要求外部文件。

【讨论】:

    猜你喜欢
    • 2020-04-26
    • 2012-02-25
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 2012-08-09
    • 2018-03-30
    • 1970-01-01
    • 2019-10-07
    相关资源
    最近更新 更多