Home > Back-end >  RewriteRules not load some of css. js and images
RewriteRules not load some of css. js and images

Time:11-06

At the moment, I'm having trouble with loading some of css, js and image files when I use RewriteRules to rewrite my URLs.

Contents of my .htaccess are:

<IfModule mod_rewrite.c>
    RewriteEngine On

    Options  FollowSymlinks
    # Options  SymLinksIfOwnerMatch

    RewriteBase /

    RewriteRule ^([^/] )/([^/] )/([^/] )/?$ news.php?newstype=$1&newsid=$2&newstitle=$3 [L,QSA,NC,B]

    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME}\.php -f 
    RewriteRule ^(.*)$ $1.php

    # RewriteRule ^(css|js|img)/?$ /$1 [L,QSA,R=301]
</IfModule>

Here is the structure of my site:

[root]
  css
    vender
      bootstrap.css
    plugin
      plugin.css
    style.css
  js
    vender
      bootstrap.js
    plugin
      plugin.js
    main.js
  images
    sliders
      slider1.jpg
      slider2.jpg
    1.jpg
    2.jgp
  index.php
  .htaccess

This is how I load my JS and CSS files:

/css/style.css
/css/vender/bootstrap.css
/css/plugin/plugin.css

/js/main.js
/js/vender/bootstrap.js
/js/plugin/plugin.js

Its load only style.css file and main.js files. Can anybody tell me, what is problem to not load other files which are inside a sub folder?

If I comment this line RewriteRule ^([^/] )/([^/] )/([^/] )/?$ news.php?newstype=$1&newsid=$2&newstitle=$3 [L,QSA,NC,B], then every thing is working nicely.

I also tried changing relative path to absolute and adding <base href="/"> inbetween the <head> </head> tags. But either doesn’t work for me.

Hope somebody may help me to figure this out.

CodePudding user response:

With your shown samples/attempts, please try following htaccess Rules file.

Please make sure to clear your browser cache before testing your URLs. Basically we need to place your rewrite rule for news.php under other rewrite rule.

Also making sure to disable MultiViews functionality in rules to make sure only exact contents are being found.

<IfModule mod_rewrite.c>
RewriteEngine On
Options  FollowSymlinks
Options -MultiViews

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.*)/?$ $1.php [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/] )/([^/] )/([^/] )/?$ news.php?newstype=$1&newsid=$2&newstitle=$3 [L,QSA,NC,B]

</IfModule>
  • Related