How to disable single file from being cached - .htaccess

I have a file called xxx.php and in htaccess i have
<ifModule mod_deflate.c>
<filesMatch "\.(js|css|html|php)$">
SetOutputFilter DEFLATE
</filesMatch>
</ifModule>
how can i disable xxx.php file being cached?

Use a negative lookahead to exclude xxx.php:
<FilesMatch "^(?!xxx\.php$).*\.(js|css|html|php)$">
SetOutputFilter DEFLATE
</FilesMatch>

Related

How in .htaccess enable gzip only for certain url of the site?

I have the site on yii2. I am using pretty URL.
And now I have the following settings for compressing:
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
# Make proxies work as they should.
<IfModule mod_headers.c>
Header append Vary User-Agent
</IfModule>
</IfModule>
How can I enable compressing for url foo1/bar1 and foo2/bar2?

.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.

Apache htaccess order of compression and cache

I have seen examples Apache htaccess file code where compression is before cache and visa versa. Which is correct? I use redbot.org quite a bit and it seems compressing after cache produces fewer issues.
You may compress with gzip compression in this way:
<ifModule mod_deflate.c>
<filesMatch ".(css|js|x?html?|php)$">
SetOutputFilter DEFLATE
</filesMatch>
</ifModule>
To cache files, you could do this:
<ifModule mod_headers.c>
<filesMatch ".(ico|jpe?g|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 ".(x?html?|php)$">
Header set Cache-Control "max-age=600, private, must-revalidate"
</filesMatch>
</ifModule>
To more informations, visit this page:
http://samaxes.com/2009/01/more-on-compressing-and-caching-your-site-with-htaccess/

.htaccess file has no effect

I have read about 10 different articles explaining how to write an .htaccess file. I've followed their explanations and put the .htaccess.txt file in the root directory. But still it won't redirect my site from non-www to www.
Here's my .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mecoder.co.il$ [NC]
RewriteRule ^(.*)$ http://www.mecoder.co.il/$1 [L,R=301]
<FilesMatch "\.(jpg|png)$">
Header set Cache-Control "public, max-age=321408000"
</FilesMatch>
<ifModule mod_headers.c>
ExpiresActive On
ExpiresDefault A86400
<FilesMatch "\.(ico|gif|jpg|jpeg\png|flv|pdf|swf|mov|mp3|wmv|ppt)$">
ExpiresDefault A1814400
Header append Cache-Control "public"
</FilesMatch>
<FilesMatch "\.(xml|txt|html)$">
ExpiresDefault A259200
Header append Cache-Control "proxy-revalidate"
</FilesMatch>
<FilesMatch "\.(js|css)$>
ExpiresDefault A10800
Header append Cache-Control "proxy-revalidate"
</FilesMatch>
<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>
<ifModule mod_deflate.c>
<FilesMatch "\.(js|css|html|htm|php|xml)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)(\.gz)?$">
Header unset ETag
FileETag None
</FilesMatch>
And here is my site
First, it's not .htaccess.txt but .htaccess (without .txt).
If that doesn't work, you must check if overriding with .htaccess file is enabled in your server configuration. To do that, you should add AllowOverride All to your vhost or apache configuration.

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

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

Resources