October 22, 2024
Chicago 12, Melborne City, USA
PHP

Relative paths do not work anymore after rewriting requests


I am new to web development and I am trying to understand how to protect certain files from user direct access.
I want to redirect all requests to "index.php" and give access to a file with sensitive information (such as an image) based on a certain condition (like the user’s credentials).
To do this, I have created an ".htaccess" file.
The path to "index.php", ".htaccess" and "image.png" is "C:\xampp\htdocs\project".
I also modified httpd.conf to "AllowOverride All" and the redirecting works fine.
The problem is that the relative path to that file does not work.

".htaccess":

RewriteEngine On
RewriteBase /project/
RewriteRule ^(.*)$ index.php

"index.php":

<body>
    <?php
        $routes = ["/project/image.png"];
        $user_logged_in = true;

        if(in_array($_SERVER['REQUEST_URI'], $routes) && $user_logged_in)
            echo "<img src=\"image.png\">";
    ?>
</body>

The image is not loading (broken image icon). How can I accomplish this?
Where can I find a beginner friendly documentation about this topic?

I modified the image path in many ways but nothing seems to work.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video