Home > Net >  Serve static files without hard coding all file types header information
Serve static files without hard coding all file types header information

Time:05-21

I am currently making a router in my project and I realised a problem. I am using the .htaccess file to redirect all routes to my router.php file. This creates a problem where all of my css/images/js files are redirected to my .htaccess file too. This is the code for my .htacess file:

RewriteEngine on
Options  Multiviews
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(. )/?$ $1.php

RewriteRule ^(.*)$ router.php [NC,L,QSA]

I had tried solving this problem by manually detecting if it is one of the recognised static file type and manually returning the contents. In my router class, I checked if the give file type is a recognised static file type. If it is a static file type I will render it if not I will give it to the handle function which basically checks if the route is recognised and returns the call to a function.

       if ($isStatic["check"]) {
            $this->render->returnStaticFiles($data["uri"], $isStatic["type"], $isStatic["ext"] ?? "");
        } else {
            $this->handle($data["uri"]);
        }

However, the problem with this is I need to manually add the static file types to an array and use a function to set the right headers or else it is just going to be recognised as text/html and that doesn't work with either css or images. Css will not get parsed and images will simply return as binary texts as gibberish. This is how I currently implement the render class:

<?php

namespace app\Router;

use JetBrains\PhpStorm\ArrayShape;

class Render
{
    public array $static_file_types = ["js", "css", "html", "json"];
    public array $image_types = ["png", "jpg", "jpeg"];

    public function __construct(
        public string $root_dir = "",
        public string $home = "http://localhost/"
    ){}

    public function throwError(int $statusCode = 500) :string
    {
        $err_dir = $this->root_dir . "errors\\";
        $file_contents = file_get_contents($err_dir . "$statusCode.php") ?? false;
        if (!$file_contents) {
            return str_replace(
                ["{code}", "{home}"],
                [$statusCode, $this->home],
                file_get_contents($err_dir . "err_template.php")
            );
        }
        return str_replace("{home}", $this->home, $file_contents);
    }
    public function returnStaticFiles(string $uri, string $type, string $ext) : void
    {
        if ($type == "cnt") {
            $this->setHeader($ext);
            include_once $this->root_dir . $uri;
        } elseif ($type == "img") {
            $this->handleImage($this->root_dir . urldecode($uri), $ext);
        } else {
            echo "This file is not supported sorry";
            exit();
        }
    }
    private function handleImage(string $path_to_image, string $type) : void
    {
        if (file_exists($path_to_image)) {
            $image = fopen($path_to_image, "r");
            header("Content-Type: image/$type");
            header("Content-Length: " . filesize($path_to_image));
            fpassthru($image);
        } else {
            echo "This image does not exist sorry";
        }
    }
    #[ArrayShape(["check" => "bool", "type" => "string", "ext" => "string"])]
    public function checkIsStatic($uri) : array
    {
        $_uri = explode(".", $uri);
        $ext = end($_uri);

        if (in_array($ext, $this->static_file_types)) {
            return ["check" => true, "type" => "cnt", "ext" => $ext];
        } elseif (in_array($ext, $this->image_types)) {
            return ["check" => true, "type" => "img", "ext" => $ext];
        } else {
            return ["check" => false, "type" => ""];
        }
    }
    private function setHeader($ext): void
    {
        switch ($ext) {
            case "css":
                header("Content-Type: text/css"); break;
            case "js":
                header("Content-Type: text/javascript"); break;
            case "json":
                header("Content-Type: application/json"); break;
            default:
                header("Content-Type: text/html"); break;
        }
    }
}

This is working just fine but is there any way around this? I might need to serve PDF content or other file types such as audio or video and I don't think this is an optimal solution for it. I tried searching it but I don't think I can find anything.

I appreciate any assistance.

CodePudding user response:

Turns out it was a simple solve. I just changed the redirect everything to router.php into another line RewriteRule !.(js|css|ico|gif|jpg|png)$ router.php [L]

This checks if the file is not a js or css or etc then it redirects to router.php file or else it would normally serve the file. I think my question is not clear enough for other people to understand so I hope this answer clears things up.

Big shoutout to this link which made everything right: rewrite rule in .htaccess: what does it do

  • Related