Pages aren't caching right, response headers seem wrong... how to fix? - .htaccess

Ok, I really didn't know how to put the question but my problem or at least I think it's the problem goes like this:
The blog I am working on loads really slow and so I did the normal things to speed it up such as add gzip and disabled all plug-ins etc. and it didn't help. So I looked at the headers being sent in Firebug and noticed the original request for the page was taking forever while the rest of the requests loaded as normal.
Here's the response/request headers for a given page:
Request Headers
Host: dev.mydomain.com
User-Agent: Mozilla/5.0... ...Firefox/3.6.17
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://dev.mydomain.com/blog/2011/05/06/hello-world/
Cookie: Cookie data...
Response Headers
Date: Tue, 07 Jun 2011 17:37:42 GMT
Server: Apache
X-Pingback: http://dev.mydomains.com/blog/xmlrpc.php
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Last-Modified: Tue, 07 Jun 2011 17:37:59 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 4981
Connection: close
Content-Type: text/html; charset=UTF-8
Obviously something is wrong here as there's no cache, connection is set to close and the expires is 30 years ago.
Here's my .htaccess file where I set my expires header etc.
.htaccess
# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/x-javascript
# Or, compress certain file types by extension:
<Files *.html|*.php>
SetOutputFilter DEFLATE
</Files>
ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/ico A2592000
ExpiresByType text/css A2592000
ExpiresByType text/javascript A2592000
ExpiresByType application/javascript A2592000
ExpiresByType application/javascript A2592000
FileETag none
AddType text/x-component .htc
AddType image/x-icon .ico
AddHandler application/x-httpd-php .php .html
DirectoryIndex index.php
allow from all
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /blog/index.php [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
## Ignore CSS, Scripts and Images
RewriteRule !\.(xml|swf|js|ico|gif|jpg|png|css|htc)$ index.php [L]
Any help if much appreciated.
BTW, I'm running WordPress 3.1.1 on PHP 5.2.10/MySQL 5.1 on CentOS 5.*

I am battling with the same issues and one thing I discovered is that your .htaccess file is only as good as the permissions given by your web host. Are you hosting your own website or are you with a webhosting company?
If you are hosting your own site you need to make sure that the 'Allow Override' directive is set properly so the .htaccess rules will work. Better yet, set up the httpd.config file to do the job for you as well.
But if you are with a webhosting company, then you need to ask them about which directives (those .htaccess rules you wrote) you can use? If your webhost has the Allow Override set to NONE, then your .htaccess file will be ignored.
Everywhere I have searched states that an Expires header set in the far future is best: when it's set in the past (Nov 1981) it forces the browser to fetch a new page/image each time.
The last thing I would suggest is to double check your .htaccess rules, here is a set that might be useful to you as a starting point:
#BEGIN htaccess
#Protect the htaccess file
<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>
#Protect the htaccess file
<Files wp-config.php>
Order Allow,Deny
Deny from all
</Files>
# Disable directory browsing
Options All -Indexes
# Enable the following of symlinks
Options +FollowSymLinks
<IfModule mod_headers.c>
# No ETags, No Pragma
Header unset Pragma
Header unset ETag
# Make sure proxies deliver correct content
Header append Vary User-Agent env=!dont-vary
# Ensure proxies deliver compressed content correctly
Header append Vary Accept-Encoding
</IfModule>
# Set up Cache Control headers
ExpiresActive On
# Default - Set http header to expire everything 1 week from last access, set must-revalidate
ExpiresDefault A604800
Header append Cache-Control: "max-age=3600, must-revalidate"
# Apply a customized Cache-Control header to frequently-updated files
<FilesMatch "^(test¦eval)\.html$">
ExpiresDefault A1
Header unset Cache-Control:
Header append Cache-Control: "no-cache, must-revalidate"
</FilesMatch>
<FilesMatch "^robots\.txt$">
ExpiresDefault A7200
</FilesMatch>
ExpiresByType image/x-icon A14515200
# Set up caching on media files for 1 month
<FilesMatch "\.(gif|jpg|JPG|jpeg|png|PNG|swf)$">
ExpiresDefault A2419200
</FilesMatch>
# Set up caching on commonly updated files 1 month
<FilesMatch "\.(xml|txt|html|js|css)$">
ExpiresDefault A2419200
</FilesMatch>
<FilesMatch "\.(ico|gif|jpg|JPG|jpeg|png|PNG|css|js|html?|xml|txt)$">
FileETag None
</FilesMatch>
<IfModule mod_deflate.c>
<FilesMatch "\.(js|css|text|html)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
Hope this has been of some help--Good Luck!
Lightfoot

Related

how optimize htaccess successfully?

i have a wordpress site and i want to optimize website by htaccess
this is my gtmetrix analyze
https://gtmetrix.com/reports/akhbartop.com/nDZRjsNK
i want optimize my website without using plugin.before i used wp-rocket but does not effect in my website as well.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
<files wp-config.php>
order allow,deny
deny from all
</files>
# Block the include-only files.
RewriteEngine On
RewriteBase /
RewriteRule ^wp-admin/includes/ - [F,L]
RewriteRule !^wp-includes/ - [S=3]
RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
RewriteRule ^wp-includes/theme-compat/ - [F,L]
#Gzip - compress text, html, javascript, css, xml
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</ifmodule>
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
#End Gzip
# BEGIN Compress text files
<ifModule mod_deflate.c>
<filesMatch "\.(css|js|x?html?|php|woff|ttf|png|jpg|gif)$">
SetOutputFilter DEFLATE
</filesMatch>
</ifModule>
# END Compress text files
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
# BEGIN Expire headers
<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType application/x-javascript "access plus 1 year"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
ExpiresByType font/ttf "access plus 1 year"
ExpiresByType font/woff "access plus 1 year"
</ifModule>
# END Expire headers
# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
<filesMatch "\.(ico|jpe?g|png|gif|swf|woff|ttf)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<filesMatch "\.(css)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<filesMatch "\.(js)$">
Header set Cache-Control "max-age=2592000, private"
</filesMatch>
<filesMatch "\.(x?html?|php)$">
Header set Cache-Control "max-age=600, private, must-revalidate"
</filesMatch>
</ifModule>
# END Cache-Control Headers
# BEGIN Turn ETags Off
<ifModule mod_headers.c>
Header unset ETag
</ifModule>
FileETag None
# END Turn ETags Off
# BEGIN Remove Last-Modified Header
<ifModule mod_headers.c>
Header unset Last-Modified
</ifModule>
# END Remove Last-Modified Header

Enable gzip on aws

I upload my angular 2 website on aws s3. Now, when I am checking page speed, It is saying to enable gzip compression to improve speed. I added .htaccess file in src folder with below code. But I can't see any effect of it.
<IfModule mod_deflate.c>
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
# Compress all output labeled with one of the following MIME-types
# (for Apache versions below 2.3.7, you don't need to enable `mod_filter`
# and can remove the `<IfModule mod_filter.c>` and `</IfModule>` lines
# as `AddOutputFilterByType` is still in the core directives).
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/atom+xml \
application/javascript \
application/json \
application/ld+json \
application/rss+xml \
application/vnd.ms-fontobject \
application/x-font-ttf \
application/x-web-app-manifest+json \
application/xhtml+xml \
application/xml \
font/opentype \
image/svg+xml \
image/x-icon \
text/css \
text/html \
text/plain \
text/x-component \
text/xml
</IfModule>
</IfModule>
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/javascript.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType text/html "access plus 1 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
</ifModule>
<ifModule mod_headers.c>
<filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<filesMatch "\\.(css)$">
Header set Cache-Control "max-age=604800, public"
</filesMatch>
<filesMatch "\\.(js)$">
Header set Cache-Control "max-age=216000, private"
</filesMatch>
<filesMatch "\\.(xml|txt)$">
Header set Cache-Control "max-age=216000, public, must-revalidate"
</filesMatch>
<filesMatch "\\.(html|htm|php)$">
Header set Cache-Control "max-age=1, private, must-revalidate"
</filesMatch>
</ifModule>
<ifModule mod_headers.c>
Header unset ETag
</ifModule>
FileETag None
<ifModule mod_headers.c>
Header unset Last-Modified
</ifModule>
Please help me to achieve it.
S3 does not support .htaccess directives.
There are two options you can use.
Option 1.
Compress your content yourself, using gzip and then set the header Content-Encoding: gzip when you upload the objects in S3. Do not use a .gz extension on the uploaded object keys.
Option 2.
Use CloudFront + S3 for your site. In the CloudFront configuration, under Cache Behavior, enable Compress Objects Automatically.

.htaccess mod_filter text/html gzip on apache 2.4

I'm trying to enable compression for Content-Type:text/html
I am running apache 2.4 and I found out that mod_gzip and mod_deflate are no longer used, but instead I should use mod_filter
https://httpd.apache.org/docs/2.4/mod/mod_filter.html
After a long google session and a lot of try / fail attempts decided to try my luck here.
I can confirm that both mod_filter and mod_deflate are enabled.
Combinations I tried to add to my .htaccess:
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE text/html
</IfModule>
<IfModule mod_deflate.c>
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE text/html
</IfModule>
</IfModule>
<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE "%{Content_Type} = 'text/html'"
FilterChain COMPRESS
FilterProtocol COMPRESS DEFLATE change=yes;byteranges=no
</IfModule>
and basically anything I could find on the internet. But nothing works.
I keep getting the same result:
Request Headers:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
Accept-Encoding:gzip, deflate, sdch
Response Headers:
Content-Type:text/html; charset=utf-8 (without the Content-Encoding:gzip header)
Using aws ec2 instance.
Found a workaround using httpd.conf. I'll just leave this here if anyone finds it useful.
And just to confirm - AddOutputFilterByType DEFLATE ... works on apache 2.4.

Google PageSpeed compressing and .htaccess

I am trying to speed up my website as best as I can. I ran Google's PageSpeed, got 83/100.
Page says to compress resources, such as .js and .css files.
"Compressing resources with gzip or deflate can reduce the number of bytes sent over the network."
Okey, then I googled and edited my .htaccess:
<IfModule mod_deflate.c>
<FilesMatch "\.(css|js|xml)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(js|css|xml|gz)$">
Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/png A604800
ExpiresByType image/gif A604800
ExpiresByType image/jpg A604800
ExpiresByType image/jpeg A604800
ExpiresByType text/javascript A604800
ExpiresByType application/x-javascript A604800
ExpiresByType text/css A604800
</IfModule>
When I inspect my site:
Headers:
Accept-Encoding gzip, deflate
Cache:
Data Size 332
Device disk
Expires Thu Jan 01 1970 02:00:00
And PHP in my site:
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
ob_start("ob_gzhandler");
else
ob_start();
header('Vary: Accept');
header('Cache-Control: max-age=28800');
Any ideas what is going wrong?
In your sample code above there's no extension specified for html content e.g. .php
I generally manage gzip by mimetype rather than by file extension e.g.
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/atom+xml \
etc
See https://github.com/h5bp/html5-boilerplate/blob/master/.htaccess for a full list.

Properly formatted caching for .htaccess file

I just wish to check If code for .htacess file is good. I wish to implement catching on site, so I found code and edited just time for my needs.
I wish to cache flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|swf files for 1 month, gif|jpg|jpeg|png|js|css files for 2 weeks, xml|txt|htm|html files for 1 day and not to cache php|cgi|pl files.
What I need is this:
Is Code ok, or there is maybe some mistake in it?
Do I need both mod_headers.c and mod_expires.c in my .htacces file, and can be both, or just one?
This is code for .htaccess file:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A86400
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A1209600
ExpiresByType text/css A1209600
ExpiresByType image/gif A1209600
ExpiresByType image/png A1209600
ExpiresByType image/jpeg A1209600
ExpiresByType text/plain A86400
ExpiresByType application/x-shockwave-flash A2592000
ExpiresByType video/x-flv A2592000
ExpiresByType application/pdf A2592000
ExpiresByType text/html A86400
</IfModule>
<ifmodule mod_headers.c>
## Turn on Expires and set default expires to 1 day
ExpiresActive On
ExpiresDefault A86400
## Set up caching on media files for 1 month
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|swf)$">
ExpiresDefault A2592000
Header append Cache-Control "public"
</FilesMatch>
## Set up caching on images css and js files for 2 weeks
<FilesMatch "\.(gif|jpg|jpeg|png|js|css)$">
ExpiresDefault A1209600
Header append Cache-Control "public"
</FilesMatch>
## Set up 1 day caching on commonly updated files
<FilesMatch "\.(xml|txt|htm|html)$">
ExpiresDefault A86400
Header append Cache-Control "private, must-revalidate"
</FilesMatch>
## Force no caching for dynamic files
<FilesMatch "\.(php|cgi|pl)$">
ExpiresDefault A0
Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
Header set Pragma "no-cache"
</FilesMatch>
</ifmodule>
And do I need to put this too
<ifModule mod_headers.c>
Header unset ETag
</ifModule>
FileETag None
And also which is better for compression to use:
<ifModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/css text/javascript application/javascript application/x-javascript
</ifModule>
or
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

Resources