views:

381

answers:

2

I am trying to build a multi-app CodeIgniter 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: www.myapp1.com/js/jquery.js

The file is located in: assets/myapp1/js

The directive 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?

thanks,

+1  A: 

You can use AliasMatch

<VirtualHost myapp1>
   AliasMatch ^(.*\.)(js|css)$ /path/to/assets/myapp1$1$2
</VirtualHost>
SpliFF
As I understand it, the same public path may map to a different private path based on the file suffix. But an Alias will map all public paths to the same internal path, no?
the.jxc
But I'm trying to redirect only the requests for js, css, and image files. All other requests needs to be channeled through the bootstrap file, as per required by the framework. Your solution would seem to apply to all requests to the root, no?
picardo
I thought Apache would map the directories on top of each other, so if a resource is not found in the alias then the root will be checked. I just tested it and that wasn't the case. However look at AliasMatch, which will let you match against a regex.
SpliFF
Answer updated for AliasMatch rather than Alias
SpliFF
I must be doing something wrong. I added the line above to the end of the VirtualHost block and restarted the server, but it's giving me a 404 error. Is there a way to debug Apache directives? I mean, I had the same problem with the solution above yours. Then I moved the js folder to the root, and sure enough, it worked. Looks like the server is not rewriting these urls at all, for whatever reason.
picardo
+3  A: 

This is totally possible. See the Apache rewrite guide:

http://httpd.apache.org/docs/1.3/misc/rewriteguide.html

Your rule will look something like this:

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

Have fun!

the.jxc
I see. Yeah, this should work, but when I try to implement this locally, though, it's not working like it should. I don't know what is wrong. Here is my .htaccess file, which is located at the root.<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} myapp1\.localhost\.dev$ RewriteRule ^/js/(.*)\.js$ assets/myapp1/js/$1 RewriteCond $1 !^(assets|.*\.js|.*\.css|.*\.png|user_guide|index\.php|robots\.txt) RewriteRule ^(.*)$ /codeigniter-171/index.php/$1 [L]</IfModule>
picardo
Just a few word of explanation, the second Rewrite rule is to redirect all requests through bootstrapper, but the same 404 error happens when I remove it as well. Also, I realize that the rule above probably needs .js at the end, after $1, no?
picardo
Found the cause of the 404. The slash in front of the js in the RewriteRule shouldn't be there. Man, 3 hours to figure out this shit.
picardo
Hiya picardo. Yeah, you're quite right... should be a .js after the $1. Can you please post the actual working rule, and I'll update my example to be correct for people in future.Sorry, I should have actually configured up apache and tested this out for you before posting. I just put together something based on the documentation... but really I should have made sure it worked 100%. My bad. :(
the.jxc