views:

197

answers:

2

I have problems with static files url rewriting in current .htaccess setup on apache2.

My app structure is:

/siteroot
/siteroot/app
/siteroot/lib
/siteroot/...
/siteroot/public <- all the static files (images, js, etc.) stored here
/siteroot/index.php
/siteroot/.htaccess

So, i need to rewrite url like /css/style.css to /public/css/style.css. I did that in really simple way, but when the file is not found it causing 10 internal redirects, which is bad. I need somehow to return 404 code if file not found, or just pass it to the next rule. And i dont have any access to site configuration file. Only .htaccess.

The reason why i`m asking this question is that the site was running on nginx and i need to rebuild the same configuration on apache.

Here is my .htaccess file.

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
+1  A: 

Test if a redirect is reasonable:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/public/$0 -f
RewriteRule ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]
Gumbo
thank you, this one solved the problem.
Dan Sosedoff
@Dan Sosedoff: Also note what Kev said. Since my solution requires two file system tests before the redirect actually happens, this is not the most cost-efficient solution.
Gumbo
A: 

If the number of prefixes is limited, you could add another group to your regex:

RewriteRule ^(css|js|images|etc)/.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]
Kev