Welcome to WebmasterWorld Guest from 34.204.191.31

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Browser caching and htaccess code

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

Senior Member

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

joined:Dec 19, 2004
posts: 993
votes: 14


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!
12:13 pm on July 15, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:12913
votes: 893


Just a FYI - google page insights often miss caching, correct or not. Test with a variety of tools.
12:26 pm on July 15, 2016 (gmt 0)

Senior Member

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

joined:Dec 19, 2004
posts: 993
votes: 14


Thanks keyplyr can you also review the code for me if you are familiar? Thanks!
12:36 pm on July 15, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:12913
votes: 893


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.
12:40 pm on July 15, 2016 (gmt 0)

Senior Member

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

joined:Dec 19, 2004
posts: 993
votes: 14


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>
12:46 pm on July 15, 2016 (gmt 0)

Senior Member

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

joined:Dec 19, 2004
posts: 993
votes: 14


Problem is I also have ExpiresDefault "access 1 month"

So it'll cache php also which I don't want to cache.
12:49 pm on July 15, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:12913
votes: 893


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]

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

Senior Member

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

joined:Dec 19, 2004
posts: 993
votes: 14


So can you post a correct code for me on this page? I appreciate your help in advance. Thanks!
12:54 pm on July 15, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:12913
votes: 893


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.
1:16 pm on July 15, 2016 (gmt 0)

Senior Member

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

joined:Dec 19, 2004
posts: 993
votes: 14


Should I remove this code also: ExpiresDefault "access 1 month"
1:27 pm on July 15, 2016 (gmt 0)

Full Member

5+ Year Member

joined:Aug 16, 2010
posts:257
votes: 23


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"
1:30 pm on July 15, 2016 (gmt 0)

Senior Member

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

joined:Dec 19, 2004
posts: 993
votes: 14


No default caching code? Also, what's the difference between ExpiresByType text/x-javascript and ExpiresByType application/javascript
1:36 pm on July 15, 2016 (gmt 0)

Senior Member

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

joined:Dec 19, 2004
posts: 993
votes: 14


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>
6:12 pm on July 15, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15893
votes: 876


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.
1:12 pm on July 17, 2016 (gmt 0)

Senior Member

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

joined:Dec 19, 2004
posts:993
votes: 14


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!
1:18 pm on July 17, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:12913
votes: 893


Use your favorite search engine for:
"page cache test"
2:41 pm on July 17, 2016 (gmt 0)

Senior Member

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

joined:Dec 19, 2004
posts:993
votes: 14


Thanks so much!
8:02 am on July 18, 2016 (gmt 0)

Senior Member

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

joined:Dec 19, 2004
posts:993
votes: 14


Also anyone used cloudflare for cdn purposes?
9:51 am on July 18, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:12913
votes: 893


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