404 功能正常但无法正常显示

404 function is working but not showing properly

所以我有包含以下代码的 .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 ^(.*)$ .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'; ?>

那么问题出在哪里呢?

你在 .htaccess 中尝试这段代码

#404页

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

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

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

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

正确请求外部文件。