Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htacces css/js subdirectory issue

I am trying to make some changes to .htaccess file of a site. I have searched all over the web, but I cant find a clear solution for that, or there is possibility that I have got it wrong. So here we are are:


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule   ^/$  /e/www/  [R]
RewriteRule ^news$ /news.php
RewriteRule ^news$ /news.php
RewriteRule ^resources$ /resources.php
RewriteRule ^lexicon$ /lexicon.php
RewriteRule ^contacts$ /contacts.php
RewriteRule ^analytics$ /analytics.php
RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)-(.*)$ /news.php?yy=$1&mm=$2&dd=$3&alias=$4
RewriteRule ^resources$ /contacts.php
RewriteRule ^articles_search$ /articles_search.php
RewriteRule ^articles_data_search$ /articles_data_search.php
RewriteRule ^profile/([0-9]+)&commit=(.*)$ /profile.php?id=$1&commit=$2
RewriteRule ^profile/(.*)&commit=(.*)$ /profile.php?alias=$1&commit=$2

At last 2 rows, i am trying to have a result from this:

www.example.com/profile.php?alias=kokazani&commit=Edit

to this:

www.example.com/profile/kokazani&commit=Edit

The problem now is the wrong path of css and js files. I need a solution, or a better approach of rewriting rules. I think that a solution ,maybe, is to rewrite everything like css/blabla.css to root directory/css/blabla.css, but if there a standard-solution foro this problem, i would like to know :)

Thanks in advance.

like image 937
kokazani Avatar asked Aug 14 '26 18:08

kokazani


2 Answers

There are 2 possible (simple) solutions:

  • Use absolute paths for your CSS files (/include/css/style.css etc)
  • Use HTML's <base> element to set the correct base URL for relative URLs to follow (rather then the directory of the document which is altered by Apache).
like image 181
Madara's Ghost Avatar answered Aug 17 '26 09:08

Madara's Ghost


My approach would be (as suggested by Truth) using absolute paths. If this is not possible, you could use:

RewriteRule ^(.+)/css/(.*)$ /css/$2

But be warned: This means the client (browser) will end up getting and caching multiple copies of the same file because it is fetching it (as far as it can see) from different paths. Therefore you lose the advantage of client-side stylesheet caching.

like image 28
daiscog Avatar answered Aug 17 '26 08:08

daiscog