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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks Olaf. I've tried both methods but none have worked unfortunately
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?
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 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?
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"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.