Home > Net >  Css and images do not load on some pages because of the htacess file
Css and images do not load on some pages because of the htacess file

Time:08-25

in my htaccess file I have the following rule:

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

and my index.php file looks like this

$url = '';
if (isset($_GET['url'])) {
    $url = explode('/', $_GET['url']);
}

if($url ==''){
    require 'home.php';

}elseif ($url[0] == 'article' AND !empty($url[1])) {
    $idArticle = $url[1];
    require 'article.php';
}elseif($url[0] == 'test'){
    require 'test.php';
}
else{
    require '404.php';
}

I have no problem on the test.php page where everything works correctly. But on my article pages the css, and the images do not load. I fixed the problem for the css with this line in my index.php:

elseif ($url[0] == 'article' AND $url[1] == 'style.css') {
   require 'style.css';

but I can't find solutions for the images. I have tried several things in the htacess but it does not work. Example :

RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.zip|\.css|\.js)$
\\\\\\\\\\\\
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]

Thanks in advance

CodePudding user response:

Place this in your html head tag <base href="https://yourdomain.com/"> It helps the page to understand where it should start looking for files After using the tag in the <head> the any src attribute will now start its search from https://yourdomain.com/the_directory_provided_in_the_attribute. This way you have an absolute path to your resources and assets

  • Related