1

So i had .htaccsess file with the following code

.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

So I want to create a restriction for users who don't have access to a certain url. When the user tries to access a certain url via the url address, the user will be redirected to a 404 page.

The function works but the 404 page doesn't display correctly, instead of using the error page from .htacess, but the standard 404 error from apache, the following code is

header function

<?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 . "");
    }
}

Then I tried using the require function, but that's also a problem because on the 404-page.php page I'm using require function too. Which make the file become to contradict. The file code is

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'; ?>

So where is the problem?

  • Why is it a problem on your 404 page to use `require`? Your 404 doesn't require any authorization? Or is there just shared code that would lead to an infinite redirect? [Including your 404 page](https://stackoverflow.com/a/41593478/231316) when you send the status code is the generally correct way to do this. – Chris Haas Nov 19 '21 at 14:12
  • well if i am using require on header function, will result the error like this `Warning: require(../koneksi/koneksi.php): failed to open stream: No such file or directory` The file is loaded, but will become to contradict because in 404-page.php i had a require function too. – AnonymousUID Nov 19 '21 at 14:14
  • Can't you just change the path then? – Chris Haas Nov 19 '21 at 14:15
  • I can't, because the koneksi file is in different folder – AnonymousUID Nov 19 '21 at 14:17
  • I mean, can't you just change the path in your `require`? You have `require(../koneksi/koneksi.php)`, just use `require(../../koneksi/koneksi.php)` or `require(koneksi/koneksi.php)` or whatever your path scheme is – Chris Haas Nov 19 '21 at 14:19
  • If i ll do that, it will result another error. What is am asking, why `header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found", true, 404);` is using default 404 error. Not using on my .htaccess – AnonymousUID Nov 19 '21 at 14:22
  • Your Apache's 404 directive will probably never kick in because of the `%{REQUEST_FILENAME}.php -f` condition (although maybe it will for directories). Instead, you have basically said that PHP is authoritative for all missing files, and Apache shouldn't weigh in on the matter. In PHP, when you send the 404, you are speaking raw HTTP to the client, Apache isn't intercepting that call and loading files for you. If you want Apache to kick in, then you need to redirect to the 404 page instead, which I personally find ugly, although it works. – Chris Haas Nov 19 '21 at 14:36
  • Hmm, am i suppose to made 1 file error-page anymore? – AnonymousUID Nov 19 '21 at 14:51
  • 1
    Just don't use relative paths for the require then, but either an absolute path, or use one of the magic constants (`__DIR__`, `__FILE__`) to help figure out "where" you currently are. – CBroe Nov 22 '21 at 07:38

2 Answers2

0

u try this code in .htaccess

#404 page

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

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 20 '21 at 09:26
0

So, i've just figured it out to solve my problem. Thanks to @CBroe at the comment to suggest me using DIR It's actually working fine for me now.

So now in the 404-page.php now i am using

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

To require the file outside properly.