silexphp / Silex

[DEPRECATED -- Use Symfony instead] The PHP micro-framework based on the Symfony Components
https://silex.symfony.com
MIT License
3.58k stars 718 forks source link

versioning of static asset files with .htaccess #907

Closed frankitoy closed 10 years ago

frankitoy commented 10 years ago

Hi, Im trying to incorporate silex into my project and Im mixing the framework with other tech which are node and grunt.

Now I hit a brickwall trying to sort how to support file versioning for my asset files, as mentioned I am leaning on using grunt to make automated testing faster and favourable, instead of using the silex assetic provider which basically just sorting out the minification process.

Now when I try to build versioning using grunt the generated link reference on twig template will be like js/main.12345.js but theoritically it should make a symbolic link to js/main.js, now it conflicts with silex since all requests is redirected to web/index.php and the reference to this js/main.12345.js file is considered as not found.

I added some line on .htaccess with this RewriteRule ^(scripts|css|images)/(.+).(.+).(js|css|jpg|gif|png)$ $1/$2.$4 [L] but it still doenst work.

For referrence http://www.particletree.com/notebook/automatically-version-your-css-and-javascript-files/

davedevelopment commented 10 years ago

I think you'll need to provide more information, but it doesn't sound like a silex problem anyway. Grunt should be placing the file in your web root, in which case the usual

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]

Should take care of things.

GromNaN commented 10 years ago

I'm working on a similar system (with gulp) and I'm using this Apache configuration:

  RewriteEngine On
  RewriteRule ^/css/(.*)_(\d*).css$       /css/$1.css
  RewriteRule ^/js/(.*)_(\d*).js$         /js/$1.js
  RewriteRule ^/images/(.*)_(\d*).(\w+)$  /images/$1.$3

  # Put the Silex catch-all after the rewrite rule
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]
fmarcelo commented 10 years ago

Hi,

Heres my .htaccess configuration:

RewriteEngine On #RewriteRule ^(.+).(\d+).(js|css|png|jpg|gif|html)$ $1.$3 [L] RewriteRule ^(js|css)/(.+).(.+).(js|css)$ $1/$2.$4 [L] ``` RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] ```

@GromNaN still I have issues because I have subdirectories inside the js directories for angular templates so I am not sure if your code snippet will actually support the needs of my app.

terite commented 10 years ago

This is not a bug in Silex, and should be closed.

The problem is most certainly a misconfiguration of Apache, or a misunderstanding of the folder structure in place. If you make a request to /js/main.js in your browser, it will look for the file at web/js/main.js. If that file does not exist, it will trigger web/index.php.

If it does exist, it will serve that file without involving web/index.php at all.