所以我有帶有以下代碼的 .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') ?> © ReD | Projects <?= $version ?></p>
</div>
</div>
</div>
</div>
</div><!-- /Page Container -->
<?php require 'footer.php'; ?>
那么問題出在哪里呢?
uj5u.com熱心網友回復:
你在 .htaccess 中試試這個代碼
#404 頁面
錯誤檔案 404 http://localhost/not-found.php
uj5u.com熱心網友回復:
所以,我只是想出來解決我的問題。感謝@CBroe 在評論中建議我使用DIR 現在實際上對我來說作業正常。
所以現在在404-page.php現在我正在使用
<?php require_once __DIR__ . '/../koneksi/koneksi.php' ?>
正確地要求外部檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/371384.html
