I've got a framework that routes all incoming URIs through a base file, and to deal with static files I've got a sub-directory /static in which I put all CSS, JS and images (ie. /static/css/main.css) in order to keep things clear.
My own code and plugins deal with this fine, but some times other code needs to be implemented, and often CSS files will try to style up with calls to URIs in those files. How can I deal with this in the best way?
An example ;
/about/company routes to /script?q=about/company and locks in the main structure of the site. However; /static/css/main.css uses a background image from; /static/images/widget/bg-color.png
Since this is a framework I'm not happy to hard-code the /static paths in the CSS files. For one, I don't want to restrict websites to only being served from some root directory. :) For all JS there's objects that deal with this (ie. var x = $xs_dir.js + '/script.js' ;) but nothing exists for CSS. I have five options, I think ;
(worst) Have an option in my admin tool that scans all CSS files for URI references, and prepends them with the right static directory, and writing all CSS as if they're static to a root directory.
(poor) Rely on the webserver's ability to alias any static directory to one root static directory, and let the admins deal with it.
(meh, slow) Serve the CSS files through the framework, filtering URIs with the right static paths.
(simplest, but not very easy) Hand-code the static portions of my CSS files for whatever server setup there might be, and just make sure they're easy to find and change.
(probably best, but complex?) Have a rewrite rule that detects images in current directory, forwarding them to the static directory, and write all CSS with some recognized dynamic path. (ie. instead of /static/images/img开发者_如何学Python.png do images/img.png and rely on rewrite rules to push it where it needs to go, also restricting the website structure to never have a sub-directory called 'images')
Any additional options? Ideas? I know Joomla and similar has some rewriting of files, and probably do no. 5?
Not sure whether you have an .htaccess file or not, but one option would be to put in a rewrite rule for request under a folder "images" to be redirected to your static (or whatever) folder:
RewriteRule ^[^\?]*(images/)(.*)$ /static/$1$2 [NC,L]
That way you can reference all your images as just /images/whatever.png in your CSS.
If you require a different setup for different servers, you could always have a separate .htaccess file for each environment or server, and version control these.
You can use the HTML base tag http://www.w3schools.com/tags/tag_base.asp to set the path for all relative url in your HTML. However, this might break all your relative link too.
It is probably a bit optimistic to demand this approach from admins, particularly if they don't control the server environment, however:
You can do this by the use of a separate (sub)domain to serve static files. You can then create a separate virtual host in your web server to serve these files. This approach also have the advantage that you can optimize for speed by using a cookie-less domain, and aggressive cache options. In addition, it allows the browser to fire up additional threads against the sub-domain, to download these files in parallel with the rest of the page.
E.G. Google is already hosting a number of open source frameworks which you can use directly, like Jquery UI with the corresponding css.
Another option for admins is to use a reverse proxy, like varnish, to serve and cache any existing file, and then pass every other get uri to the backend server containing framework controller (then the framework files, except for 'router', should be outside of the public folder for security).
I guess that at least having the option to serve static files from a cookie-less domain would be handy, else Duffmanss answer should suffice.
Assuming you're routing everything to 'script.php' this would route all non-existing files to script.php, thus allowing static content to be served directly, while routing to your controllers as well.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ script.php [QSA,L]
If all the CSS files end up in /static/css/
and all the images which the CSS references end up in /static/images/
, then just make sure that whenever you reference an image from CSS that you use a relative path ../images/widget/bg-color.png
and it should work fine.
Option 5 works with Apache. If all your image references in your css files start with a relative subdir without a preceding slash (eg. url(images/image.png)
), then in <css_dir>
you can rewrite images/
to <where_the_images_are>
. Your .htaccess
stays in, and only applies to <css_dir>
along with the css files and work from wherever you dropped it.
The RewriteRule
only needs to know that images/
relative to itself is the one that matters.
精彩评论