Apache with mod_rewrite in use and FilesMatch - apache

In my .htaccess file I have:
<FilesMatch "\.(js|css|png|gif|jpg)$">
<IfModule mod_headers.c>
Header set Cache-Control "public, max-age=31536000"
Header set Expires "A31536000"
</IfModule>
</FilesMatch>
I also have a rewrite for /forum-js/forum.js -> /wp-content/plugins/forum/js/forum.js.php, this is the only file that should match the above regex. For some reason, all other resources matching that regex have headers that include Cache-Control and Expires, but this one does not. Is it because it's a rewritten url?

Is it because it's a rewritten url?
Yup, as per your comment, its real extension is .php and the rules associated with that extension are in effect.
The easiest thing would be to send the headers from within the PHP script:
<? header("Cache-Control: public, max-age=31536000");
header("Expires : pA31536000");

You could also use mod_expires instead:
<IfModule mod_expires.c>
<FilesMatch "\.(js|css|png|gif|jpg)$">
ExpiresDefault A31536000
</FilesMatch>
</IfModule>
It also works with MIME media types instead of file name extensions:
<IfModule mod_expires.c>
ExpiresByType application/javascript A31536000
ExpiresByType text/css A31536000
ExpiresByType image/png A31536000
ExpiresByType image/gif A31536000
ExpiresByType image/jpeg A31536000
</IfModule>

Related

htaccess: cache all images but exclude caching images from subfolder

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.

Apache cache control failure

Please i need help so badly, i have turned on apache cache control on some files types especially font files using both expires module and header module (Not simultaneously). Every file specified caches except for font file, i have tried my best possible to fix it but no luck. Any solution to fix this?
Expires module method
##___________Cache control________________________________________##
# Declare fonts content-type
AddType application/vnd.ms-fontobject .eot
AddType application/x-font-ttf .ttf
AddType application/x-font-opentype .otf
AddType application/x-font-woff .woff
AddType image/svg+xml .svg
ExpiresActive on
ExpiresByType application/x-font-woff "access plus 7200 seconds"
ExpiresByType application/x-font-ttf "access plus 7200 seconds"
ExpiresByType application/vnd.ms-fontobject "access plus 7200 seconds"
ExpiresByType application/svg+xml "access plus 7200 seconds"
Header append Cache-Control "public"
##________________________________________________________________________##
Header module method
##___________Cache control________________________________________##
<filesmatch "\.(ttf|jpg|svg|png|jpeg|ico|js)$">
Header set Cache-Control "max-age=86400, public"
</filesmatch>
##________________________________________________________________________##
Output
Instead of this:
<filesmatch "\.(ttf|jpg|svg|png|jpeg|ico|js)$">
Header set Cache-Control "max-age=86400,
public"
</filesmatch>
Try this:
<FilesMatch regex>
</FilesMatch>

Different htaccess in http or https

I have a problem. I use cloudflare free plan and robots.txt is different if i put http:// or https://. In http: // comes the robots.txt that I want to use, in the https: // another totally different from a web that is hosted on the same host as me.
I really have no idea why that file does not redirect the http to https and another thing that does not work specifically to open this file is the not www to www. I leave details of my htaccess. A greeting and I hope someone can help me.
# Use UTF-8 encoding for anything served text/plain or text/html
AddDefaultCharset UTF-8
# Force UTF-8 for a number of file formats
<IfModule mod_mime.c>
AddCharset UTF-8 .atom .css .js .json .rss .vtt .xml
</IfModule>
# FileETag None is not enough for every server.
<IfModule mod_headers.c>
Header unset ETag
</IfModule>
# Since we’re sending far-future expires, we don’t need ETags for static content.
# developer.yahoo.com/performance/rules.html#etags
FileETag None
# Send CORS headers if browsers request them; enabled by default for images.
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
# mod_headers, y u no match by Content-Type?!
<FilesMatch "\.(cur|gif|png|jpe?g|svgz?|ico|webp)$">
SetEnvIf Origin ":" IS_CORS
Header set Access-Control-Allow-Origin "*" env=IS_CORS
</FilesMatch>
</IfModule>
</IfModule>
# Allow access to web fonts from all domains.
<FilesMatch "\.(eot|otf|tt[cf]|woff2?)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
<IfModule mod_alias.c>
<FilesMatch "\.(html|htm|rtf|rtx|txt|xsd|xsl|xml)$">
<IfModule mod_headers.c>
Header set X-Powered-By "WP Rocket/2.10.3"
Header unset Pragma
Header append Cache-Control "public"
Header unset Last-Modified
</IfModule>
</FilesMatch>
<FilesMatch "\.(css|htc|js|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$">
<IfModule mod_headers.c>
Header unset Pragma
Header append Cache-Control "public"
</IfModule>
</FilesMatch>
</IfModule>
# Expires headers (for better cache control)
<IfModule mod_expires.c>
ExpiresActive on
# Perhaps better to whitelist expires rules? Perhaps.
ExpiresDefault "access plus 1 month"
# cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5)
ExpiresByType text/cache-manifest "access plus 0 seconds"
# Your document html
ExpiresByType text/html "access plus 0 seconds"
# Data
ExpiresByType text/xml "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType application/json "access plus 0 seconds"
# Feed
ExpiresByType application/rss+xml "access plus 1 hour"
ExpiresByType application/atom+xml "access plus 1 hour"
# Favicon (cannot be renamed)
ExpiresByType image/x-icon "access plus 1 week"
# Media: images, video, audio
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# HTC files (css3pie)
ExpiresByType text/x-component "access plus 1 month"
# Webfonts
ExpiresByType application/x-font-ttf "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType application/x-font-woff "access plus 1 month"
ExpiresByType application/x-font-woff2 "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
# CSS and JavaScript
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
# Gzip compression
<IfModule mod_deflate.c>
# Active compression
SetOutputFilter DEFLATE
# Force deflate for mangled headers
<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
# Don’t compress images and other uncompressible content
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png|rar|zip|exe|flv|mov|wma|mp3|avi|swf|mp?g|mp4|webm|webp)$ no-gzip dont-vary
</IfModule>
</IfModule>
# Compress all output labeled with one of the following MIME-types
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/atom+xml \
application/javascript \
application/json \
application/rss+xml \
application/vnd.ms-fontobject \
application/x-font-ttf \
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 mod_headers.c>
Header append Vary: Accept-Encoding
</IfModule>
</IfModule>
# 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
Here is a something that works for www and non-https redirections. Put it in your <IfModule mod_rewrite.c>, at the beginning:
RewriteEngine On
# Redirecting non-www and non-SSL
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

.htaccess Disable cache-control mod_expires and mod_headers for paths that do not have physical directory

I have Laravel application that is using mod_expires, mod_headers, and mod_gzip in the public folder to enable caching on the frontend of the site. However, in the admin panel this is breaking some of the functionality.
I would like to disable/exclude the paths to the admin panel which are generated using mod_rewrite. I would have just put a .htaccess file in a folder with the same name as path, but this causes issues as well.
The path to the admin panel would be "/manage".
Here's what the .htaccess file looks like at this time.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Secure Front Themes...
RewriteRule ^themes/.*\.(blade.php|php)$ - [F,L,NC]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "max-age=2692000, public"
</FilesMatch>
<FilesMatch "\\.(css)$">
Header set Cache-Control "max-age=2692000, 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>
Header unset ETag
Header unset Last-Modified
</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/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
Thanks in advance

VirtualHost working only in home page with Codeigniter

I'm facing some problems with Virtual Hosts under Apache 2.2.
I want to host development versions of some sites in my local environment (some of them using codeigniter) and be able to access them using a ".dev" extension, so I can go to:
- http://myproject.dev to access the local version or
- http://myproject.com to access production version
I've set up some name-based virtual hosts as follows:
NameVirtualHost localhost
<VirtualHost localhost>
DocumentRoot ".../workspace.web/"
ServerName localhost
</VirtualHost>
NameVirtualHost myproject.dev
<VirtualHost myproject.dev>
DocumentRoot ".../workspace.web/myproject.com/htdocs"
ServerName myproject.dev
</VirtualHost>
and set my hosts file
127.0.0.1 psychologuenice.dev
The home page is working fine, but subdirectories are crashing in codeigniter. For example
myproject.dev/whatever
returns a 404 error.
I'm not sure if any of the rules in htaccess could be causing this, or is just due to a bad understanding of what I'm doing here. My htaccess looks like this:
#RewriteEngine on
#RewriteCond $1 !^(index\.php|robots\.txt|images|css|js|captcha|inc|html|tmp|captcha)
#RewriteRule ^(.*)$ /index.php/$1 [QSA,L]
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
# BEGIN Compress text files
<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>
# END Compress text files
# BEGIN Expire headers
<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 5 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType application/x-shockwave-flash "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"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>
# END Expire headers
# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
<filesMatch "\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(css)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(js)$">
Header set Cache-Control "private"
</filesMatch>
<filesMatch "\.(x?html?|php)$">
Header set Cache-Control "private, must-revalidate"
</filesMatch>
</ifModule>
# END Cache-Control Headers
# BEGIN Turn ETags Off
FileETag None
# END Turn ETags Off
Could someone give any clue?