Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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.

share|improve this question
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 at 13:37
    
@Zistoloen not by the moment.. –  tirengarfio May 13 at 13:38
    
href="styles.css" is obviously a relative path, but I assume is loaded at the root of your site? –  w3d May 13 at 14:17

3 Answers 3

up vote 5 down vote accepted

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]
share|improve this answer

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.

share|improve this answer

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.)

share|improve this answer
    
@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? –  tirengarfio May 13 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. –  Stephen Ostermiller May 13 at 13:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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