Skip to content
Advertisement

My php rooter is routing my css/jpg… files

I got a homemade php rooter with an htaccess rewriting rule but my public files are always getting catched by the rooter after I leave my landing page, blocking thoses sources on my other pages.

index.php rooter :

if(isset($_GET['url']) && !empty($_GET['url'])){

    // Explosion of the URL
    $url = explode('/', $_GET['url']);



    $controllerName = "Controllers\".ucfirst(array_shift($url)).'Controller';

    $methodName = strtolower(array_shift($url));
    $param=strtolower(array_shift($url));

    $controller = new $controllerName;
    if($param!=null){
        $controller->$methodName($param);
    }

    else{
        $controller->$methodName();
    }

}

else{
    header("Location: Views/landing.php");
}

My htaccess :

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [NC,L]

I should maybe change the htaccess ? but how ?

Thx

Advertisement

Answer

Your client-side resources should be referenced like this:

<link href="/css/Dashboard/custom.css" type="text/css" rel="stylesheet" />
<link href="/images/favicon/favicon.ico" rel="icon" type="image/png" />
<script src="/bootstrap/dist/js/bootstrap.bundle.min.js" type="text/javascript"></script>

etc. So, note the prepended slash character (/) in the HTML attributes href and src. It references the location of the document root: /path/to/sample-mvc/public.

And, just as an example: here is the virtual host of my MVC-based app. Maybe it helps.

<VirtualHost *:80>
    ServerName local.myapp
    DocumentRoot "/path/to/sample-mvc/public"

    <Directory "/path/to/sample-mvc/public">
        # For developing allow just the development machine 
        # (presuming that all dirs & files are inaccessible by default).
        Require ip 127.0.0.1

        # When Options is set to "off", then the RewriteRule directive is forbidden!
        Options FollowSymLinks
        
        # Activate rewriting engine.
        RewriteEngine On
        
        # Allow pin-pointing to index.php using RewriteRule.
        RewriteBase /
        
        # Rewrite url only if no physical folder name is given in url.
        RewriteCond %{REQUEST_FILENAME} !-d
        
        # Rewrite url only if no physical file name is given in url.
        RewriteCond %{REQUEST_FILENAME} !-f
        
        # Parse the request through index.php.
        # Notice the absence of query string ("?url=...").
        # I use "FastRoute" as router.
        RewriteRule ^(.*)$ index.php [QSA,L]
    </Directory>
</VirtualHost>
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement