Forum Moderators: phranque

Message Too Old, No Replies

Browser caching and htaccess code

         

born2run

11:46 am on Jul 15, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi I have tried adding browser caching to my htaccess. I placed the following code into my htaccess:

<IfModule mod_expires.c>
# Enable expirations.
ExpiresActive On

# Cache all files for 2 weeks after access (A).
#ExpiresDefault A1209600
ExpiresByType text/css "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresDefault "access 1 month"

<FilesMatch \.php$>
# Do not allow PHP scripts to be cached unless they explicitly send cache
# headers themselves. Otherwise all scripts would have to overwrite the
# headers set by mod_expires if they want another caching behavior. This may
# fail if an error occurs early in the bootstrap process, and it may cause
# problems if a non-Drupal PHP file is installed in a subdirectory.
ExpiresActive Off
</FilesMatch>
</IfModule>

My questions are: Is this the right code for browser caching? I checked google page insights but the results show that it detects no browser caching even though I've added the code above. So there must be something wrong in the code.

Can anyone please help? Thanks so much in advance!

keyplyr

12:13 pm on Jul 15, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just a FYI - google page insights often miss caching, correct or not. Test with a variety of tools.

born2run

12:26 pm on Jul 15, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks keyplyr can you also review the code for me if you are familiar? Thanks!

keyplyr

12:36 pm on Jul 15, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm on a mobile phone with a limited keyboard that can't handle RegEx but it appears you may have some redundancies.

I would just list all file types as 30 days and leave out php. One simple rule.

born2run

12:40 pm on Jul 15, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks so I should remove the following code:


<FilesMatch \.php$>
# Do not allow PHP scripts to be cached unless they explicitly send cache
# headers themselves. Otherwise all scripts would have to overwrite the
# headers set by mod_expires if they want another caching behavior. This may
# fail if an error occurs early in the bootstrap process, and it may cause
# problems if a non-Drupal PHP file is installed in a subdirectory.
ExpiresActive Off
</FilesMatch>

born2run

12:46 pm on Jul 15, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Problem is I also have ExpiresDefault "access 1 month"

So it'll cache php also which I don't want to cache.

keyplyr

12:49 pm on Jul 15, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes... But also know that browsers have defalt caching and there is no way to effectively stop them from caching. There are several methods of NOCACHE floating around the internet, none work well.

Networks and ISPs also may cache. Mobile apps may cache.

[edited by: keyplyr at 1:05 pm (utc) on Jul 15, 2016]

born2run

12:51 pm on Jul 15, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So can you post a correct code for me on this page? I appreciate your help in advance. Thanks!

keyplyr

12:54 pm on Jul 15, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As I said... I'm on a phone.

Just list all file types as 30 days and leave out php. Remove everything else. Then test.

born2run

1:16 pm on Jul 15, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Should I remove this code also: ExpiresDefault "access 1 month"

bhukkel

1:27 pm on Jul 15, 2016 (gmt 0)

10+ Year Member



I have the following code in my httpd.conf

ExpiresActive On
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/css "access plus 1 month"

born2run

1:30 pm on Jul 15, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No default caching code? Also, what's the difference between ExpiresByType text/x-javascript and ExpiresByType application/javascript

born2run

1:36 pm on Jul 15, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok now I have this code in my .htaccess :

<IfModule mod_expires.c>
# Enable expirations.
ExpiresActive On
ExpiresByType text/css "access 1 month"
ExpiresByType application/javascript "access 1 month"

<FilesMatch \.php$>
# Do not allow PHP scripts to be cached unless they explicitly send cache
# headers themselves. Otherwise all scripts would have to overwrite the
# headers set by mod_expires if they want another caching behavior. This may
# fail if an error occurs early in the bootstrap process, and it may cause
# problems if a non-Drupal PHP file is installed in a subdirectory.
ExpiresActive Off
</FilesMatch>
</IfModule>

lucy24

6:12 pm on Jul 15, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Instead of the extra <FilesMatch> business, can't you just set
ExpiresByType text/php "access"
?

Funny this should come up today. Last night I was exploring one of those page-speed tools (not Google) and it kept yapping about caching static content. I cannot for the life of me understand why it thinks piwik.php, a well-known analytics package, is "static content" when by its very nature it has to be re-requested each time and there's an enormous query string that always changes.

born2run

1:12 pm on Jul 17, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi I've set up browser caching on my site using the . Htaccess file.

Is there any reliable way to find out if it's working properly? Thanks!

keyplyr

1:18 pm on Jul 17, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use your favorite search engine for:
"page cache test"

born2run

2:41 pm on Jul 17, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks so much!

born2run

8:02 am on Jul 18, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Also anyone used cloudflare for cdn purposes?

keyplyr

9:51 am on Jul 18, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@ born2run - best to start a new thread when you change the topic so members will see it and respond.