问题
I have been working on a CakePHP App, most of the CSS / JS / images (icons) are located in the webroot directory (/var/www/app/webroot/), but for some portion (Croogo) or other pages of the app, I need to load CSS / JS / images from a different directory i.e /var/www/app/views/themed/admin/webroot/.
Now the issue I am facing is that when I render these other pages the css / JS / images requests return 404 page not found status, as these requests are made to main webroot rather than the different directory i mentioned above.
Is there a way to handle this with .htaccess, i.e if the url contains this pattern "/admin/"
Example:
http://www.myapp.com/theme/admin/css/mycss.css
The CSS / JS / img files should be looked up into /var/www/app/views/themed/admin/webroot/ rather than var/www/app/webroot/ ??
If not is there any other workaround to handle such a scenario?
Following is the .htaccess of main app directory
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Following is the .htaccess of main webroot directory
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
CakePHP code
echo $html->link($html- >image('../theme/admin/img/icons/16/icon16x16.png',array('alt'=>'','class'=>'icon left','id'=>'profile-icon')).'Resize',array('action'=>'edit_photo','01'),array('escape'=>false,'id'=>'profile-photo','class'=>'blue','rel'=>'shadowbox;height=100;width=400','title'=>'Resize'));
Generated HTML
<a href="/edit_photo/01" id="profile-photo" class="blue" rel="shadowbox;height=100;width=400" title="Resize"><img src="/theme/admin/img/icons/16/icon16x16.png?v=1000" alt="" class="icon left" id="profile-icon" />Resize</a></div>
CakePHP version 1.3
Any thoughts and ideas would be appreciated!
回答1:
That's not how it works
Nothing outside the webroot is supposed to be web accessible.
Theme assets should be in the webroot
Theme files are not stored in the webroot, but they are intended to be either copied/linked to the webroot - or served via the asset dispatching process.
In this specific case that means for example:
app
views
themed
admin
webroot
webroot
theme
admin -> ../../views/themed/admin/webroot # <- a symlink
Note that this means that with the correct theme asset url - it maps directly to a file (There is no CakePHP or PHP logic in serving it).
You can also rely on asset dispatching using the correct url (in 1.3 this is automatic, in later versions it needs to be enabled explicitly) - though that is significantly slower.
Use "absolute" urls in your application code
In the question a relative url is used:
echo $html->image('../theme/admin/img/icons/16/icon16x16.png'
^
This is quite fragile and a possible factor in why the resultant url does not point at the right file - instead use an absolute url (within your application, an absolute url is always relative to wherever it has been installed):
echo $html->image('/theme/admin/img/icons/16/icon16x16.png'
^
This ensures the right url irrespective of the current controller/action/location.
来源:https://stackoverflow.com/questions/23357123/css-js-images-not-loading-from-a-different-directory-than-main-webroot-in-ca