3

I have in a remote server with this index.html that I can not modify:

<link rel="stylesheet" type="text/css" href="styles.css" /> 

But my styles.css file is not at the web root, that is /styles.css but at /css/styles.css, is possible through an .htaccess file load that file?

I have tried with:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /css/
</IfModule>

but it styles.css is not loaded yet.

NOTE: of course this is a minimal example, I would like to find a rule to load more than a CSS, not just one.

3
  • 1
    It's weird to have rights to modify an .htaccess file without having rights to modify the index.html which is in the same place. Are you sure you can't modify the index.html?
    – Zistoloen
    May 13, 2014 at 13:37
  • @Zistoloen not by the moment..
    – tirenweb
    May 13, 2014 at 13:38
  • href="styles.css" is obviously a relative path, but I assume is loaded at the root of your site?
    – MrWhite
    May 13, 2014 at 14:17

3 Answers 3

6

Although the browser should cache permanent redirects, I think an internal rewrite would be preferable. This avoids the browser having to make an additional HTTP request:

RewriteEngine On
RewriteRule ^([a-z]+\.css)$ /css/$1 [L]
4

You could just redirect /styles.css to /css/styles.css. In the .htaccess in the web root:

RedirectMatch permanent /([a-z]+\.css)$ http://example.com/css/$1

(Replacing example.com with your real domain name.)

2
  • @StephenOstemiller thanks, but as I said in my question I have many .css files, how to avoid writting the full list of the .css files?
    – tirenweb
    May 13, 2014 at 13:50
  • I updated the answer with a RedirectMatch directive that would redirect all the css files that are in the root directory to the css folder. May 13, 2014 at 13:58
2

Just as an alternative to the .htaccess rules, you could include a styles.css file in the root, and just put @import statements in there that load the actual css files. Something like this:

/styles.css

@import 'css/styles1.css';
@import 'css/styles2.css';
...

I know it is an extra request and definitely not best practice, but it should be cached, and is a flexible and easy solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.