2

I create .htaccess in root project and add this code for set expire date for css and js.

My server is Ubuntu 16.04

But this code not work for my website.

This code in htaccess:

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType image/x-icon "access plus 2592000 seconds"
  ExpiresByType image/jpeg "access plus 2592000 seconds"
  ExpiresByType image/png "access plus 2592000 seconds"
  ExpiresByType image/gif "access plus 2592000 seconds"
  ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 216000 seconds"
  ExpiresByType application/x-javascript "access plus 216000 seconds"
  ExpiresByType text/html "access plus 600 seconds"
  ExpiresByType application/xhtml+xml "access plus 600 seconds"
</IfModule>

<IfModule mod_headers.c>
<FilesMatch "\\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "max-age=2692000, public"
</FilesMatch>
<FilesMatch "\\.(css)$">
Header set Cache-Control "max-age=2692000, public"
</FilesMatch>
<FilesMatch "\\.(js)$">
Header set Cache-Control "max-age=216000, private"
</FilesMatch>
<FilesMatch "\\.(x?html?|php)$">
Header set Cache-Control "max-age=600, private, must-revalidate"
</FilesMatch>
Header unset ETag
Header unset Last-Modified
</IfModule>

How do I fix the problem?

Thank you

5
  • Are mod_expires and/or mod_headers installed on your server? Remove the <IfModule> wrappers - do you get an error? "My server is Ubuntu 16.04" - since you've included your .htaccess file, I assume you are on Apache? Which version? Both your rule blocks are doing the same sort of thing - why are you using both mod_expires and mod_headers - is this code intended to work on different servers? "in Laravel" - since Laravel is a PHP framework, if you wanted to do this "in" Laravel implies you want to do this "in PHP" - but I assume that's not the case?
    – MrWhite
    Mar 20, 2017 at 16:06
  • Hi @w3dk, Thank you for help me. I now run mod_expires in apache :-) Please create answer for like your answer :-)
    – mySun
    Mar 20, 2017 at 16:13
  • Is it now working? (I didn't really have an answer, only questions?)
    – MrWhite
    Mar 20, 2017 at 16:18
  • @w3dk , Yes now i see expire date in header :-) Thank you.
    – mySun
    Mar 20, 2017 at 16:24
  • Maybe you can add an answer yourself stating what you did to resolve the problem? (You can later accept it.)
    – MrWhite
    Mar 20, 2017 at 16:27

1 Answer 1

1

You need to ensure that mod_expires is actually enabled. On Ubuntu, it comes shipped with Apache2, but it is not enabled by default. To enable it, run the commands:

sudo a2enmod expires
sudo service apache2 restart

a2enmod is the apache2 script that enables a module. If you want to disable the module, you would use a2dismod instead.

After the module has been enabled, Apache2 much be either restarted, or it needs to reload its config files. The second command does the latter.

Your Answer

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

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