Enable mod_expires for browser caching - apache

The htaccess file looks like this:
# 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>
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType image/jpg "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
# CSS
ExpiresByType text/css "access plus 1 week"
# Javascript
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType text/html "access plus 1 day"
</IfModule>
# END WordPress
Any reason why this wouldn't enable browser caching? I am running Centos 5 and have made sure the expires module is installed.

Try to use this... by file extension... change the max-age for your needs
and even use DEFLATE to compress the download size
# 1 YEAR = 29030400 || 2 DAYS = 172800 || 1 MIN = 60
# 1 WEEK = 604800
<filesMatch "\.(jpg|jpeg|png|gif|swf|xml|txt|css|js|ico|pdf|flv|html|htm)$">
Header set Cache-Control "max-age=604800, public"
</filesMatch>
#compress
<filesMatch "\.(js|css|json|html|xml)$">
SetOutputFilter DEFLATE
</filesMatch>

Related

'403 Forbidden error' when trying to access images on a Wordpress install

I have a dedicated server. I have installed wordpress in a subdirectory on it and uploaded some images via the 'media' option in the dashboard. But I am unable to access images directly (hotlinks) in the browser, however, I can see the images in the file manager (FTP) under /wp-content/uploads folder.
It is a fresh install with no plugins and only the default theme. I haven't done anything other than upload a few images via the media option. Please note that the error shows even when I click the 'library' option under the 'media' option in the dashboard.
I even tried creating a .user.ini file in both the directories with the exact same code as of php.ini as shown below, but that didn't help either. The hosting provider says they don't provide support for dedicated servers. What do I do now?
Here is the root directory .htaccess
<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>
<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>
# 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
<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>
Here is the Wordpress directory .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /laksh/3/wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /laksh/3/wordpress/index.php [L]
</IfModule>
# END WordPress
Here is the php.ini code in both the directories:
file_uploads = On
upload_max_filesize = 100M
post_max_size = 100M
The uploaded images won't even display in pages or posts.
Worth checking if
RedirectMatch 403 (?i)/+upload
is the cause of your images not being displayed, the wordpress path for uploaded images, containers the string upload

301 redirects are adding ?page=

I've set up some 301 redirects in my .htaccess file... for files that don't exist. They are indeed redirecting but with an added query. For example:
Using one of my redirects on my htaccess file below:
if I type in example.com/about-example.php it should redirect me to example.com/about-example.html and it kind of is but it takes me to example.com/about-example.html?page=about-example.php with this added ?page=(redirect) and moz is seeing that redirected page with a query at the end as a duplicate to the original page.
Here is what my htaccess looks like... (changing a lot to example/xyz)
Options -Indexes +FollowSymLinks
RewriteEngine on
RewriteBase /
# Remove "www."
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Redirect any requests for html files to index
RewriteRule ^(.+)\.html index.php?page=$1 [L]
# Rewrite any request for subdirectories to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
## SITE REFERRER BANNING
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*example\.net/ [NC,OR]
## EXPIRES CACHING ##
# 6 month for most static assets
<filesMatch ".(jpg|jpeg|png|gif|gs|ico)$">
Header set Cache-Control "max-age=1576800, public"
</filesMatch>
# 1 month for most static assets
<filesMatch ".(css|js)$">
Header set Cache-Control "max-age=2628000, public"
</filesMatch>
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 1 month"
# CSS
ExpiresByType text/css "access plus 1 year"
# Data interchange
ExpiresByType application/json "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType text/xml "access plus 0 seconds"
# Favicon (cannot be renamed!)
ExpiresByType image/x-icon "access plus 1 week"
# HTML components (HTCs)
ExpiresByType text/x-component "access plus 1 month"
# HTML
ExpiresByType text/html "access plus 0 seconds"
# JavaScript
ExpiresByType application/javascript "access plus 1 year"
# Manifest files
ExpiresByType application/x-web-app-manifest+json "access plus 0 seconds"
ExpiresByType text/cache-manifest "access plus 0 seconds"
# Media
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# Web feeds
ExpiresByType application/atom+xml "access plus 1 hour"
ExpiresByType application/rss+xml "access plus 1 hour"
# Web fonts
ExpiresByType application/font-woff2 "access plus 1 month"
ExpiresByType application/font-woff "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
ExpiresByType application/x-font-ttf "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
</IfModule>
Redirect 301 /about-example.php http://example.com/about-example.html
Redirect 301 /vacation-faq.php http://example.com/faqs.html
Redirect 301 /3d-tour.html http://example.com/plans.html
Redirect 301 /images/cool/04.jpg http://example.com/gallery.html
Am I missing a command, or are the redirects in the wrong place? I have tried rewrite rules, redirectmatch... and a bunch of other things with either the same result or 500 internal errors.. (edit) I have also messed with the placement of the redirects to no avail..
You need to update your rule and add a ? at the end. Try these rules instead.
Options -MultiViews
RewriteEngine on
#if request is a real file or dir do nothing
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
#else do these redirects
RewriteRule ^about-example\.php http://example.com/about-example.html? [R=301,L]
RewriteRule ^vacation-faq\.php http://example.com/faqs.html? [R=301,L]
RewriteRule ^3d-tour\.html http://example.com/plans.html? [R=301,L]
RewriteRule ^images/cool/04\.jpg http://example.com/gallery.html? [R=301,L]
Let me know how this answer works for you.

Apache server, two cache control max-age values

Im trying to set up cache control for a site running on Ubuntu 16.04 using an Apache server. This is the .htaccess file for the site:
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymLinks
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
When checking the headers in Chrome dev tools for an image, I see two max-age values:
Cache-Control:max-age=60, public, max-age=31536000
Content-Type:image/jpeg
It seems like the first value is overriding the second value because no resources on my site are being cached for more than a minute. Where is the first value of 60 being defined?

htaccess rewrite part of url and leave rest as it is

I need to rewrite part of my url as a parameter and part as a path. Is it possible and if so can anyone please help me
To be more specific I have an url like this:
www.mysite/products/producer:some+producer/category:some+category/color:red,blue,yellow
and what I need is
www.mysite/products.php/producer:some+producer/category:some+category/color:red,blue,yellow
To make things more interesting depending on user query path after www.mysite/products/ can by shorter or longer
If I had constant number of parameters I would write something like that
RewriteRule ^products/([a-zA-Z-_0-9]+)/([a-zA-Z-_0-9]+)/?$ products.php/$1/$2 [L]
but as a number of parameters changes I have no idea what to do
That's my whole htaccess so far
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.pl
RewriteRule ^(.*)$ http://www.mysite.pl/$1 [R=permanent,L]
RewriteRule ^products/(.+)/?$ /products.php/$1 [NE,L]
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
AddDefaultCharset UTF-8
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 1 month"
Header append Cache-Control "public"
</IfModule>
## EXPIRES CACHING ##
Method #1 just changes products and keeps everything else the same
RewriteRule ^products/$ products.php [NC,L,B,QSA]
Method #2
RewriteRule ^products/producer:(.*)/category:(.*)/color:(.*)$ products.php?producer=$1&category=$2&color=$3 [NC,L]
And here's probably what your htaccess file should look like.
Options -MultiViews
RewriteEngine On
# Prevents directory browsing
Options -Indexes
# BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# END GZIP
# your 404 page
ErrorDocument 404 /path/to/your/404file.php
RewriteRule ^products/$ products.php [NC,L,B,QSA]
You can just use a .+ check:
RewriteRule ^products/(.+)/?$ /products.php/$1 [NE,L]
Putting the floating directives in their respective module checks:
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.pl
RewriteRule ^(.*)$ http://www.mysite.pl/$1 [R=permanent,L]
RewriteRule ^products/(.+)/?$ /products.php/$1 [NE,L]
</IfModule>
AddDefaultCharset UTF-8
<IfModule mod_filters.c>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
</IfModule>
</IfModule>
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 1 month"
Header append Cache-Control "public"
</IfModule>
## EXPIRES CACHING ##

Leverage Browser Caching in site

I have developed my site using magento and anguar js. From google page Insights, I got that I have to leverage browser caching. So for that, I have defined my .htaccess file lke this:
RewriteEngine On
Options FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet)
RewriteRule ^(.*)$ story.php?id=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_USER_AGENT} !facebookexternalhit/[0-9]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /#/$1 [L]
<IfModule mod_headers.c>
# YEAR
<FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf)$">
Header set Cache-Control "max-age=29030400"
</FilesMatch>
# WEEK
<FilesMatch "\.(js|css|swf|woff)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
# 45 MIN
<FilesMatch "\.(html|htm|txt)$">
Header set Cache-Control "max-age=86400"
</FilesMatch>
Header set Connection keep-alive
</IfModule>
But this is not working as the google insight is still showing the same message.
You should use mod_expires to check which assets should be reloaded after a certain time. In your .htaccess file you can configure it like so:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 2 days"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType text/javascript "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/json "access 1 month"
</IfModule>
As you can see there is an ExpiresDefault of 2years from access time on. For all images and javascripts you can spot the time. If you need other values you can find extensive docs here http://httpd.apache.org/docs/current/mod/mod_expires.html