Sunday, April 3, 2011

Mod Rewrite: apply to all js and css files except ...

Hi

I need to apply a minify actions to all the javascript and CSS files, except the ones I indicate.

I have this condition and rule that applies to all the files (css and js):

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*\.)(js|css)$ minify.php?q=$1$2 [L,NC]

I need to add the conditions to say:

Apply to all except: jquery.js, prototype.js, etc..

From stackoverflow
  • Try this

    RewriteCond %{REQUEST_FILENAME} !^.*jquery.js$
    RewriteCond %{REQUEST_FILENAME} !^.*prototype.js$
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(.*\.)(js|css)$ minify.php?q=$1$2 [L,NC]
    

    The key to this is the inversion of the regex using the "!" (exclamation point) to say the file name is not jquery.js and not prototype.js and it can be found on the hard drive.

  • You can use RewriteCond to specify on which conditions the RewriteRule should be applied. So prepend these lines to your .htaccess:

    RewriteCond %{REQUEST_FILENAME} !jquery.js
    RewriteCond %{REQUEST_FILENAME} !prototype.js
    
  • Thank you so much for this! I've been searching for this answer to my problem for almost a month. My problem was how to rewrite some files to https and the rest to http, but I was able to modify above to make it work. Here's a chunk of my code. (I have a lot more files in the real .htaccess on the site.)

    #################################################################
    # To only change certain files from http to https, the rest from 
    # https to http. Absolute links are set up throughout the site, 
    # but if a visitor for some reason edits the URL in the address
    # bar, the URL will change back to the proper format.
    
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^alvingolf\.com$ [NC]
    RewriteCond %{HTTPS} on
    RewriteCond %{SERVER_PORT} ^443$ 
    RewriteCond %{REQUEST_FILENAME} !^.*contact-us.html$
    RewriteCond %{REQUEST_FILENAME} !^.*register-for-tournaments.html$
    RewriteCond %{REQUEST_FILENAME} !^.*form.htm$
    

0 comments:

Post a Comment