0

I'm having a problem with a rewrite rule that I'm using for auto versioning of my CSS and JS.

I have the following HTML code to include my stylesheet:

<link rel="stylesheet" type="text/css" href="includes/css/index_home.1364215354.css" />

I use this in my .htaccess to strip the dot and the 10 digits:

RewriteEngine On
RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L]

When I load the site, the stylesheet fails to load even though if I point my browser to http://www.example.com/includes/css/index_home.css I can see the stylesheet.

The method I'm trying to use, I found here How to force browser to reload cached CSS/JS files?

I'm guessing the RewriteRule is the problem, but I don't know why.

halfer
  • 19,824
  • 17
  • 99
  • 186
Damian
  • 1,652
  • 4
  • 26
  • 44

1 Answers1

1

The RewriteRule is fine so far. Maybe the problem lies in the relative URL and the request has some prefix.

If this is the case, you must be more specific (start with includes) and also don't anchor at the beginning of the request string. Also rewrite it to an absolute URL path

RewriteRule (includes/.*)\.[\d]{10}\.(css|js)$ /$1.$2 [L]

If you have multiple .htaccess files, the more specific (css) overrides the .htaccess nearer to the root directory. To merge these .htaccess files, you must use RewriteOptions Inherit or RewriteOptions InheritBeforein the css .htaccess file.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Thanks Olaf. I've tried both methods but none have worked unfortunately – Damian Mar 25 '13 at 14:45
  • I've looked at my server logs and I'm getting the following error - File does not exist: /mysite.co.uk/httpdocs/includes/css/index_home.1364215354.css does that mean the RewriteRule isn't working or is that a red herring? – Damian Mar 25 '13 at 14:48
  • I've tried that too Olaf and still no joy. I've also been speaking to my web host and they have suggested it may be something to do with my server setup. I have 'FastCGI application' selected under 'php support' rather than 'apache module'. I'm not sure if this is what is causing the problem though? – Damian Mar 25 '13 at 15:31
  • @Damian Unlikely. This is just a different way how your PHP scripts are invoked. Since you have access to the server logs, you can see in the `access.log` what is actually requested. I guess `httpdocs` is your `DocumentRoot`. Where is the `includes` directory located? – Olaf Dietsche Mar 25 '13 at 15:42
  • Yes, httpdocs is the root and includes is directly below that. I've just had a look at the access.log and I can see the requested file is "GET /includes/css/index_home.1364215354.css HTTP/1.1" – Damian Mar 25 '13 at 15:53
  • @Damian Then the RewriteRule should just work. There are several reasons, why it does not. 1) mod_rewrite is not compiled in or not activated 2) You're not allowed to use it in .htaccess, see `AllowOverride` 3) You have other RewriteRules which interfere with this one. 4) Any others? – Olaf Dietsche Mar 25 '13 at 16:00
  • I've just tried with a stripped down htaccess so literally all I had in there was 'RewriteEngine On RewriteRule (includes/.*)\.[\d]{10}\.(css|js)$ /$1.$2 [L]' and I still don't get the styles applied. If I add my other RewriteRules back in after this one, they work ok. I don't get it??? – Damian Mar 25 '13 at 16:20
  • I've just found another htaccess in the css folder. Could this be what is causing the problem? The htaccess contains 'RewriteEngine on AddType "text/css" .gz AddEncoding gzip .gz RewriteCond %{HTTP:Accept-Encoding} gzip RewriteCond %{REQUEST_FILENAME}.gz -f RewriteRule (.*)\.css$ $1\.css.gz [L]' – Damian Mar 25 '13 at 16:29
  • hmm, doesn't look like it is the problem...I temporarily deleted the htaccess file in the css folder but it still doesn't work :( – Damian Mar 25 '13 at 16:31
  • I was wrong...it IS the second htaccess file that is stopping this from working!!! Now, how to make them play together nicely.... – Damian Mar 25 '13 at 16:37
  • @Damian The second .htaccess overrides the one in the root directory. See updated answer. – Olaf Dietsche Mar 25 '13 at 16:41
  • RewriteOptions Inherit sorted it! Thanks for your patience and your help :) – Damian Mar 25 '13 at 16:59