3

I am trying to follow this tutorial that says i can make a server to send a file like style.css if the requested file is style.15458888.css with a rewrite rule to be put inside the htaccess file.

This Rule

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+).(d+).(js|css)$ $1.$3 [L]

So i followed with this in the head tag:

<?php $time = filemtime(get_template_directory() .'/assets/css/main.css');?>
<link href="<?php echo get_template_directory_uri().'/assets/css/main.'.$time.'.css'?>" rel="stylesheet" type="text/css">

and this inside the Htaccess:

    # BEGIN WordPress
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 6 hours"
    ExpiresByType image/jpeg "access plus 6 hours"
    ExpiresByType image/gif "access plus 6 hours"
    ExpiresByType image/png "access plus 6 hours"
    ExpiresByType text/css "access plus 6 hours"
    ExpiresByType application/pdf "access plus 1 week"
    ExpiresByType text/javascript "access plus 6 hours"
    ExpiresByType text/html "access plus 10 minutes"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 3 hours"
</IfModule>
Header set X-Endurance-Cache-Level "2"
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^(.+).(d+).(js|css)$ $1.$3 [L]
</IfModule>

# END WordPress

As you can see the RewriteRule ^(.+).(d+).(js|css)$ $1.$3 [L] has been added before the </IfModule>

And the file name changed correctly having this:

<link href="http://ask.prosentra.com/wp-content/themes/tutorialblog/assets/css/main.1504604028.css" rel="stylesheet" type="text/css">

But still the server doesn't implement the rule i mentioned. What is wrong?

2 Answers 2

0

This is a dead end approch as the enqueue API do not support such versioning format which means that you will have to avoid using it, which is not a great thing. Your specific problem here is that you added the rule too late, RewriteRule . /index.php [L] "transfers" handling to wordpress and nothing is going to be done after that, which means that you need to switch the order of them. And again changing the wordpress core rewrite rules is not smart, you should probably add a specific section outside of the wordpress rules section.

4
  • Thank you and i know that the best approach is to use query string but i didn't know that it is not possible to do it by rewrite rules Sep 23, 2017 at 8:28
  • @MohamedOmar It is "possible to do it by rewrite rules" (whether that is recommended or not is another matter), but as MarkKaplun states, in your case you've just put the directive in the wrong place (and you've omitted the condition).
    – MrWhite
    Sep 23, 2017 at 9:31
  • @MohamedOmar it is not that you can not do, you just can not use the wordpress enqueue API "as is" to generate the kind of URLs you use in the question, because it will always use the ver parameter. Maybe my answer is actually not very correct as you can probably override how you generate the CSS and JS URLs... maybe I need to think a little more about that :) Sep 23, 2017 at 9:32
  • I tried to put the pattern as you said but also didn't get the desired result, Please correct the htaccess to the right one .. i just need to know how to make use of this technique somewhere else Sep 23, 2017 at 11:37
0

You needed only to add some \'s and reorder your rules to have this before the catch-all rule (have your rule above the WordPress rule).

You want a \ before the periods in this case because you want it to match literal periods in the http request.

You want a \ before the d because we want it to match any digit, not the letter d.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.(\d+)\.(js|css)$ $1.$3 [L]

With this we can add any arbitrary digits between the periods in the file name request which will bust cache, but it will remain the same file name on the server.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.