I made a RESTful application, which uses path-like URLs to access different data. The rule I used was very simple - all URLs to non-existent files are redirected to my main framework init script, and it handles them from there, as RESTful requests. It all works as expected - i type "http://mysite.org/do-something" and my framework gets "/do-something" as $_SERVER['REQUEST_URI']. Good. Now something a litlle more complex: "http://mysite.org/category1/do-something". The script works flawless, but generated HTML fails to load stylesheets, images and javascript. It looks for them in wrong path, which can be fixed by using <base> tag, however <base> tag prevents site from using relative links like anchor links. I'd rather avoid using the <base> tag.

In my framework there is an advanced auto-loader which finds all used scripts (it's fast because path configuration is cached and refreshed only on changes). It works, and it doesn't generate performance costs greater than other common auto-loaders. So far so good.

I invented a solution to use RESTful URLs without <base> tag: to redirect all HTML requested files to PHP auto-loader, which returns the files via exit(file_get_contents($filename)).

I have a serious question: wouldn't it be a great performance cost if used to load images?

Is there a way to do it via .htaccess alone? Maybe script generated .htaccess would be the best? Is there a way to include a file in .htaccess? In case of any write error to this file a security risk may appear.

BTW, I expect moderate site load (about 1K users per day).

  • What is preventing you from using absolute paths for stylesheets, images and scripts? If the resource is going to be the same for every request, it only makes sense to use an absolute path. – danlefree Oct 1 '10 at 12:54
  • You know what? You're 100% right! It was so obvious still I didn't see it :) THANKS DUDE, it helped a lot! – Harry Oct 2 '10 at 15:08
  • Well, it doesn't work good. I use relative URLs for site navigation and relative image paths for user content (the site has to be portable, I test sites on my domains then clients launch them on theirs). Since I removed base tag site breaks after selecting a category (which makes server "think" it's in a subdir). We gotta find a solution quick, my deadline is Monday :/ The easiest is to replace the base tag, but come on, base tag is lame. – Harry Oct 2 '10 at 19:01

There is a performance hit if apache is required to invoke a PHP script to serve images. You can avoid static files from being directed to your index.php in .htaccess:

RewriteEngine on
<any further rules>
RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php
|improve this answer|||||
  • Thanks, that's what I thought. I have to learn more about Apache to optimize my framework. Maybe is there a way to prevent server from changing base dir on paths in URLs? But that's another question. – Harry Oct 2 '10 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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