The GWT perfect caching documntation (http://www.gwtproject.org/doc/latest/DevGuideCompilingAndDebugging.html#perfect_caching) suggests to add the following lines to my .htaccess file:
<Files *.nocache.*>
ExpiresActive on
ExpiresDefault "now"
Header merge Cache-Control "public, max-age=0, must-revalidate"
</Files>
<Files *.cache.*>
ExpiresActive on
ExpiresDefault "now plus 1 year"
</Files>
As I'm not using .htaccess files but have access to the Apache 2.2 httpd.conf file I would prefer to add those lines there.
But where / how?
Thanks for any advice.
The documentation for the <Files> directive clearly states where you can use it:
Context: server config, virtual host, directory, .htaccess
In most cases, you'd probably want to add it to your application's virtual host (if you want the caching rules to apply only to that application) or the server's config (outside any directive, usually in httpd.conf) - if you want to apply those rules globally (useful if you have more than one GWT application on the server).
If you want to use it in a virtual host directive:
<VirtualHost *:80>
ServerName host.example.com
#...
<Files *.nocache.*>
ExpiresActive on
ExpiresDefault "now"
Header merge Cache-Control "public, max-age=0, must-revalidate"
</Files>
<Files *.cache.*>
ExpiresActive on
ExpiresDefault "now plus 1 year"
</Files>
</VirtualHost>
If you want to use them globally, just put them in httpd.conf, outside of any directives.
Related
I want to cache all images for 1 month and it works great but the problem is when I try to exclude a subdirectory from caching.
So there are images on:
/ (base dir)
/IMG/
/IMG/folder/
IMG/BIG/
so all images need to be cached, but i want to make it to not cache images that are on IMG/BIG/ folder
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
</IfModule>
the above code works but when
i try this code to exclude /IMG/BIG then it doesn't work
<Directory "/IMG/BIG">
<FilesMatch "\.(jpg|png)$">
<IfModule mod_expires.c>
ExpiresActive Off
</IfModule>
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>
</FilesMatch>
</Directory>
I want to fix this in the .htaccess that is in the root folder and not by adding another .htaccess inside /IMG/big folder
ExpiresByType image/jpg "access plus 1 month"
Aside: The correct mime-type for JPEG files is image/jpeg, not image/jpg, so the above probably isn't doing anything.
The <Directory> directive is not permitted in .htaccess - this should have resulted in a 500 Internal Server Error (the details of which would be in your server's error log).
The <IfModule> wrappers are not required, unless you intend to use the same config on multiple servers where mod_expires and/or mod_headers are not enabled (unlikely).
To target everything else except the /IMG/BIG subdirectory then you can use an <If> expression and check against the REQUEST_URI server variable with a negated regex (!~ operator). For example:
<If "%{REQUEST_URI} !~ m#^/IMG/BIG/#">
ExpiresActive on
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
</If>
To specifically override any headers for requests to /IMG/BIG/ then you could add an <Else> directive:
<Else>
<FilesMatch "\.(jpg|png)$">
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</FilesMatch>
</Else>
Unless there are other file types in the /IMG/BIG/ directory that you might want cached then you could remove the <FilesMatch> container.
I'm trying to use the .htaccess file on my Apache server.
Here is what my .htaccess looks like
# 1 YEAR
<FilesMatch "\.(ico|svg|woff|eot|ttf)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>
# 1 WEEK
<FilesMatch "\.(jpg|png|gif|css|js)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# Add correct content-type for fonts
AddType application/vnd.ms-fontobject .eot
AddType font/ttf .ttf
AddType font/otf .otf
AddType font/x-woff .woff
AddType image/svg+xml .svg
# Compress compressible fonts
AddOutputFilterByType DEFLATE font/ttf font/otf image/svg+xml
I enabled mod_rewrite using a2enmod rewrite
The tutorials I followed told me to edit the file in /etc/apache2/sites-available/default but there isn't a file by the name default in that folder. There was a 000-default.conf instead in the same path. But that file didn't have the part
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
which I'm supposed to edit.
This is the contents of 000-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
However /etc/apache2/apache2.conf had the exact same part. So I replaced AllowOverride None over there to AllowOverride All. I restarted the server after that.
However the .htaccess file is still not being loaded. If I add gibberish to the .htaccess file everything still works fine meaning it didn't get loaded.
What did I miss here?
First of all, the name of the file doesn't really matters. 000-default.conf, while not really common seems good to me.
Modify your site configuration
There are 2 major things you have to consider when serving .htaccess files :
AccessFileName .htaccess
AllowOverride All
As said in the apache2 doc, you must declare AllowOverride in a Directory section. That's why it didn't work when setting it in main config file.
I'd advise you to just paste those 4 lines in /etc/apache2/000-default.conf inside the Virtualhost section and it should work:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
(assuming your root directory is /var/www)
You don't have to set AccessFileName as default is .htaccess.
If it doesn't work and you have more than 1 file in /etc/apache2/sites-available
Apache might use another config thant 000-default.conf. Just check which of those are symlinked to /etc/apache2/sites-enabled. If there's still more than 1, you might want to disable every other enabled site just to be sure.
If it still doesn't work, just check file ownership & permissions.
The isssue was that I was placing the .htaccess file in /var/www/ while my DocumentRoot for my website was pointing to a different directory. Moving the .htaccess file to that folder solved the issue.
I am trying to install SugarCRM Enterprise 7.2 and I keep getting this error during system check:
Test for .htaccess rewrites failed. This usually means you do not have
AllowOverride set up for Sugar directory.
I am running Ubuntu 14.04 with LAMP, and I am trying to install Sugar to the local path
var/www/html/sugar_ent_7
I have already tried putting both
<Directory /var/www/ >
Allowoverride All
Order allow,deny
Allow from all
</Directory>
and
<Directory /var/www/html/sugar_ent_7 >
Allowoverride All
Order allow,deny
Allow from all
</Directory>
to my
/etc/apache2/apache2.conf
file, and no results. I have also tried making a
/etc/apache2/sites-available/sugar_ent_7.conf
file with this code
<Directory /var/www/html/sugar_ent_7>
Order allow,deny
Allow from All
AllowOverride All
</Directory>
and still nothing. What am I doing wrong?
Run the command
a2enmod rewrite;
in your terminal and restart your apache.
Hope this helps.
I met the same problem.
its aim is just to check the following code exists in your .htaccess:
# install/installSystemCheck.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase {$basePath}
RewriteRule ^itest.txt$ install_test.txt [N,QSA]
</IfModule>
I don't think SugarCRM is enough to check this setting, so let's just comment it out and make it passed:
#
// if($res != "SUCCESS") {
if(false) {
....
This error can also (misleadingly?) occur when sugar tries to address itself vi IP during the system check. When vhosting is configured it cannot find itself via IP, thus an entry to the hosts file can solve this. In the terminal enter:
vi /etc/hosts
Then add the IP of the system Sugar is running on with the URL you're referring to it, e.g.:
123.234.123.234 subdomain.host.com
# BEGIN SUGARCRM RESTRICTIONS
RedirectMatch 403 (?i).*\.log$
RedirectMatch 403 (?i)/+not_imported_.*\.txt
RedirectMatch 403 (?i)/+(soap|cache|xtemplate|data|examples|include|log4php|metadata|modules)/+.*\.(php|tpl)
RedirectMatch 403 (?i)/+emailmandelivery\.php
RedirectMatch 403 (?i)/+upload
RedirectMatch 403 (?i)/+custom/+blowfish
RedirectMatch 403 (?i)/+cache/+diagnostic
RedirectMatch 403 (?i)/+files\.md5$
# END SUGARCRM RESTRICTIONS
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /dir
RewriteRule ^cache/jsLanguage/(.._..).js$ index.php?entryPoint=jslang&module=app_strings&lang=$1 [L,QSA]
RewriteRule ^cache/jsLanguage/(\w*)/(.._..).js$ index.php?entryPoint=jslang&module=$1&lang=$2 [L,QSA]
</IfModule>
<FilesMatch "\.(jpg|png|gif|js|css|ico)$">
<IfModule mod_headers.c>
Header set ETag ""
Header set Cache-Control "max-age=2592000"
Header set Expires "01 Jan 2112 00:00:00 GMT"
</IfModule>
</FilesMatch>
<IfModule mod_expires.c>
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
</IfModule>
I've checked my plesk control panel on my vps and those mods are installed but when I run the site through http://redbot.org/ to check what's being sent, I get:
This response is negotiated, but doesn't have an appropriate Vary header.
The max-age Cache-Control directive appears more than once.
Cache-Control: no-cache, max-age=0, must-revalidate, no-transform, max-age=300
So it doesn't look like it's working.
Here's the .htaccess that I edited:
### SILVERSTRIPE START ###
<Files *.ss>
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Files>
<Files web.config>
Order deny,allow
Deny from all
</Files>
# This denies access to all yml files, since developers might include sensitive
# information in them. See the docs for work-arounds to serve some yaml files
<Files *.yml>
Order allow,deny
Deny from all
</Files>
# Define some expiry header settings.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 5 minutes"
ExpiresByType image/gif "access plus 7 day"
ExpiresByType image/png "access plus 7 day"
ExpiresByType image/jpg "access plus 7 day"
ExpiresByType image/jpeg "access plus 7 day"
ExpiresByType image/ico "access plus 7 day"
ExpiresByType text/css "access plus 7 day"
ExpiresByType text/javascript "access plus 7 day"
ExpiresByType application/x-javascript "access plus 7 day"
</IfModule>
# Append the 'Vary: Accept-Encoding' for resources that might need it.
<IfModule mod_headers.c>
<FilesMatch "\.(js|css|xml|gz)$">
Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>
ErrorDocument 404 /assets/error-404.html
ErrorDocument 500 /assets/error-500.html
<IfModule mod_rewrite.c>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine On
RewriteBase '/'
RewriteRule ^vendor(/|$) - [F,L,NC]
RewriteRule silverstripe-cache(/|$) - [F,L,NC]
RewriteRule composer\.(json|lock) - [F,L,NC]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule .* framework/main.php?url=%1&%{QUERY_STRING} [L]
</IfModule>
### SILVERSTRIPE END ###
I'm just trying to add a little client-side caching to my site and I was also advised to add the old Vary:Accept-Encoding for clients that can't handle gzip - apparently it's best practice to.
Any ideas where I'm going wrong here?
Your htaccess file has
<IfModule mod_expires.c>
It may be that your VPS doesn't have the mod_expires installed for apache. If that is the case then it would make sense that the expires configuration wouldn't get applied.
You can login as root to your server and run something like the 2 lines below (which might be a bit different depending on your server's config, apache version etc)
a2enmod expires
a2enmod headers
Then simply restart your Apache server:
systemctl restart apache2
or
service httpd restart
The above example works on Ubuntu 20.04 Server, running Virtualmin as a hosting solution. (tested myself)
For other installations, apache versions simply google: "a2enmod expires" on CentOS 7, Apache version 1
All I am trying to do is:
rewrite /static/styles/min.css to /static/styles/min.css.gz
rewrite /static/scripts/min.js to /static/scripts/min.js.gz
The trick is that those files are on a remote (public) server which I'm reverse proxying to.
I am doing this so I can workaround the same-origin issue with our javascript, and to speed up delivery in general. The .gz files already exist.
No matter what I do, I cannot request the .js file and have the .gz file returned.
I have tried this with numerous different RewriteConds to no avail.
I have also tried it with RequestHeader unset Accept-Encoding enabled, and commented out.
Google PageSpeed keeps telling me that it is not receiving the compressed versions, and when I request using curl and manually setting the "Accept-Encoding: gzip, deflate" header, I continue to receive the non-compressed versions. I cannot put the rewrites in the .htaccess file because the reverse proxy is processed before the .htaccess, and I need the rewrite to already be in effect when the reverse proxy happens. I'm at a total loss.
Here is my non-production setup (I know it needs securing):
<VirtualHost *:80>
ServerName ww.test.com
DocumentRoot "/htdocs/public"
Options +MultiViews
AddEncoding x-gzip .gz
AddEncoding gzip .gz
RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteRule ^\.js$ $1\.js\.gz [L]
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteRule ^\.css$ $1\.css\.gz [L]
<FilesMatch .*\.css\.gz>
ForceType text/css
Header append Content-Encoding gzip
</FilesMatch>
<FilesMatch .*\.js\.gz>
ForceType text/javascript
Header append Content-Encoding gzip
</FilesMatch>
ProxyRequests off
ProxyPass /static/ http://www.ourCDN.com/ourAccount/environmentName/
<Location /static/>
ProxyPassReverse /
#RequestHeader unset Accept-Encoding
</Location>
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
</Directory>
</VirtualHost>
FilesMatch rules apply only to files on disk - a proxied request isn't a file on disk and therefore won't be captured by a FilesMatch rule.
You probably want
<LocationMatch "^/static/.*\.css\.gz$">
ProxyPassReverse /
....
</LocationMatch>