Welcome Guest, Not a member yet? Register   Sign In
HOWTO: rewrite requests for images, css and js to different folders for each application?
#1

[eluser]picardo[/eluser]
(SOLVED. Scroll down to see the solution.)

I am trying to build a multi-app site, where the assets for all apps will be stored in a single folder called "assets." Inside this single folder, each app would have its own asset folder. So, if this is my root directory...

user_guide
apps_folder (this is where all my application folders live)
system
assets

...the assets folder will be organized thus:

assets
myapp1
js
img
css
media


Here is my challenge. I am trying to write an apache directive that would rewrite all requests for files ending in css, js, png and gif to the respective asset directory for that app. But the user cannot know the exact location of those assets in the server. To give you an example:

Here is the request: myapp1.localhost.dev/js/jquery.js

The file is located in: assets/myapp1/js

The directive I am hoping to write will check for
a) the domain address
b) file extension

...and based on those variables, will rewrite the url, thereby hiding thee real location of the asset, and make it possible to reference assets as if they were all located at the root of the website. Has anyone ever done something like this?


I have tried this"


RewriteCond %{HTTP_HOST} myapp1\.localhost\.dev$
RewriteRule ^/js/(.*)\.js$ assets/myapp1/js/$1

But it didn't work.

thanks,

EDIT: I did manage to make the lines above to work. Turns out there was a syntax error. I am posting the final result here to guide others.

########
###Each request to an asset is redirected to the application's own folder inside "assets."
###Reproduce and modify the two lines below for every domain.
######## myapp1
RewriteCond %{HTTP_HOST} myapp1\.localhost\.dev$
RewriteRule ^(js|css|img|media)/(.*)\.(js|css|gif|swf|png|jpg|ico)$ /assets/myapp1/$1/$2.$3
########


########
###Redirect all requests except these through the bootstrap file.
########
RewriteCond $1 !^(assets|.*\.js|.*\.css|.*\.gif|.*\.swf|.*\.png|.*\.jpg|.*\.ico|user_guide|index\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
#2

[eluser]slowgary[/eluser]
What about an asset controller instead? It'd probably be slightly more overhead than mod_rewrite, but would be easier (for me anyways) to figure out. I'd probably store the assets above the root, then in my controller I'd check the url to know which app/folder/file to grab. The added benefit to this method is that you could secure hotlinking by defining a constant in your applications and checking for it in the assets controller.
#3

[eluser]picardo[/eluser]
Yeah, that's true, but I'm not sure about going that route for the reason that it requires a separate solution for referring to the assets from other assets. For instance, there could be several js files that need to refer to and load each other; they can only find each other using absolute paths. So, that could be a problem.
#4

[eluser]slowgary[/eluser]
That would only be a problem though if you were checking a constant. The controller would still work otherwise for loading assets from assets.
#5

[eluser]picardo[/eluser]
Hmm. I'm not sure I get it. How would you write a controller that can control the requests made to javascript files? I'm new to CodeIgniter, but I assumed controllers are able to load templates located in views folder. Your suggestion is to write a controller named...how would you even name it? And its functionality would be to send the requests to js, css and image files to the assets folder, which is outside of the web root? I'm kind of with you, but there are still some missing pieces in my mind. How would the controller capture requests for assets, for one thing?

Though the .htaccess seems easier (if I can get it to work, argh!), this is pretty intriguing.
#6

[eluser]slowgary[/eluser]
I don't completely understand your rewrite as I'm not very good with them. Are your apps each on their own subdomain? Or a subfolder? Or do they each have a different domain and they're just sharing a server?

I pictured a controller like this:
Code:
<?php

class Assets extends Controller
{
    function Assets()
    {
        parent::Controller();
        $this->load->helper('file');
        $this->load->helper('download');
    }

    function _remap()
    {
        if(defined(APP_NAME))
        {
            $type = $this->uri->segment(2);
            $file = $this->uri->segment(3);

            if($data = read_file('/realpath/'.APP_NAME."/$type/$file"))
            {
                force_download($file, $data);
            }
        }
    }
}

/* End of controller */

Then you'd define APP_NAME in your apps as the folder where that app's assets are in. You would then access your assets at:
Code:
/assets/js/filename.js
#7

[eluser]picardo[/eluser]
I put each app on a different domain. They are sharing the same system folder. So the htaccess file checks for the domain request is coming from, and if it's a request for an asset, it redirects it to the assets folder of that app. They're on the same server. One thing I realized is that it needs tweaking to exclude user_guide, but that's not that important.




Theme © iAndrew 2016 - Forum software by © MyBB