I was trying to disable all .htaccess files on this site for Apache performance reasons (makes a server run faster) by moving rewrite rules etc… from .htaccess files to the Apache httpd.conf file.
The attempt failed due to W3 Total Cache plugin Page Cache generated .htaccess files (hundreds of them), but I did manage to move all W3 Total Cache Browser Cache .htaccess Rules to the Apache httpd.conf file.
Also moved the browser cache rules in a way which meant all ~100 domains on the server used the same browser cache rules without having to install the W3 Total Cache plugin on all the domains. Means I only had to set the W3 Total Cache Browser Cache options once and they worked for all the ~100 domains on the server: a serious time saver for those with a network of domains.
W3 Total Cache Plugin Browser Cache Rules
The W3 Total Cache plugin is a VERY popular WordPress caching plugin, it has multiple cache relevant features including the Browser Cache module.
The Browser Cache module creates a set of .htaccess file directives/rules which allow web browsers (Google Chrome, FireFox etc…) to locally cache different file types (CSS, JS, JPG files etc…) for different time periods and sets relevant web page headers.
The browser cache time periods are important SEO wise, Google considers page speed a ranking factor and a well thought out set of browser cache rules can make a site run faster. When a website lacks suitable browser cache rules, page speed tools like Google’s Lighthouse Audits Tool will generate a relevant warning.
What the Browser Cache module adds to a .htaccess file can be generated manually, there’s nothing special or specific to the W3 Total Cache Plugin, so some of the steps below can be used if your browser cache rules are generated another way (could even copy mine below).
Generating the Browser Cache .htaccess Rules
The W3 Total Cache Plugin Browser Cache .htaccess rules are easily moved from the .htaccess file to the main Apache httpd.conf, but first I had to generate them.
I installed and activated the W3 Total Cache plugin.
I setup the W3 Total Cache plugin Browser Cache options by turning the Browser Cache on via the General Settings page:
Go to “Performance” > “General Settings” : “Browser Cache” Enable Box Ticked.
Followed by settings the Browser Cache options:
“Performance” > “General Settings” > “Browser Cache“.
After all the Browser Cache options are set and saved, download the root .htaccess file and load it in a text editor and find the two lines of code # BEGIN W3TC Browser Cache and # END W3TC Browser Cache, what’s between those two lines are your sites browser cache rules:
# BEGIN W3TC Browser Cache
Browser cache rules here...
# END W3TC Browser Cache
Below are what was in this sites .htaccess file between the two lines above:
# BEGIN W3TC Browser Cache
<IfModule mod_mime.c>
AddType text/css .css
AddType text/x-component .htc
AddType application/x-javascript .js
AddType application/javascript .js2
AddType text/javascript .js3
AddType text/x-js .js4
AddType text/html .html .htm
AddType text/richtext .rtf .rtx
AddType image/svg+xml .svg
AddType text/plain .txt
AddType text/xsd .xsd
AddType text/xsl .xsl
AddType text/xml .xml
AddType video/asf .asf .asx .wax .wmv .wmx
AddType video/avi .avi
AddType image/bmp .bmp
AddType application/java .class
AddType video/divx .divx
AddType application/msword .doc .docx
AddType application/vnd.ms-fontobject .eot
AddType application/x-msdownload .exe
AddType image/gif .gif
AddType application/x-gzip .gz .gzip
AddType image/x-icon .ico
AddType image/jpeg .jpg .jpeg .jpe
AddType image/webp .webp
AddType application/json .json
AddType application/vnd.ms-access .mdb
AddType audio/midi .mid .midi
AddType video/quicktime .mov .qt
AddType audio/mpeg .mp3 .m4a
AddType video/mp4 .mp4 .m4v
AddType video/mpeg .mpeg .mpg .mpe
AddType application/vnd.ms-project .mpp
AddType application/x-font-otf .otf
AddType application/vnd.ms-opentype ._otf
AddType application/vnd.oasis.opendocument.database .odb
AddType application/vnd.oasis.opendocument.chart .odc
AddType application/vnd.oasis.opendocument.formula .odf
AddType application/vnd.oasis.opendocument.graphics .odg
AddType application/vnd.oasis.opendocument.presentation .odp
AddType application/vnd.oasis.opendocument.spreadsheet .ods
AddType application/vnd.oasis.opendocument.text .odt
AddType audio/ogg .ogg
AddType application/pdf .pdf
AddType image/png .png
AddType application/vnd.ms-powerpoint .pot .pps .ppt .pptx
AddType audio/x-realaudio .ra .ram
AddType image/svg+xml .svg .svgz
AddType application/x-shockwave-flash .swf
AddType application/x-tar .tar
AddType image/tiff .tif .tiff
AddType application/x-font-ttf .ttf .ttc
AddType application/vnd.ms-opentype ._ttf
AddType audio/wav .wav
AddType audio/wma .wma
AddType application/vnd.ms-write .wri
AddType application/font-woff .woff
AddType application/font-woff2 .woff2
AddType application/vnd.ms-excel .xla .xls .xlsx .xlt .xlw
AddType application/zip .zip
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css A31536000
ExpiresByType text/x-component A31536000
ExpiresByType application/x-javascript A31536000
ExpiresByType application/javascript A31536000
ExpiresByType text/javascript A31536000
ExpiresByType text/x-js A31536000
ExpiresByType text/html A86400
ExpiresByType text/richtext A86400
ExpiresByType image/svg+xml A86400
ExpiresByType text/plain A86400
ExpiresByType text/xsd A86400
ExpiresByType text/xsl A86400
ExpiresByType text/xml A86400
ExpiresByType video/asf A31536000
ExpiresByType video/avi A31536000
ExpiresByType image/bmp A31536000
ExpiresByType application/java A31536000
ExpiresByType video/divx A31536000
ExpiresByType application/msword A31536000
ExpiresByType application/vnd.ms-fontobject A31536000
ExpiresByType application/x-msdownload A31536000
ExpiresByType image/gif A31536000
ExpiresByType application/x-gzip A31536000
ExpiresByType image/x-icon A31536000
ExpiresByType image/jpeg A31536000
ExpiresByType image/webp A31536000
ExpiresByType application/json A31536000
ExpiresByType application/vnd.ms-access A31536000
ExpiresByType audio/midi A31536000
ExpiresByType video/quicktime A31536000
ExpiresByType audio/mpeg A31536000
ExpiresByType video/mp4 A31536000
ExpiresByType video/mpeg A31536000
ExpiresByType application/vnd.ms-project A31536000
ExpiresByType application/x-font-otf A31536000
ExpiresByType application/vnd.ms-opentype A31536000
ExpiresByType application/vnd.oasis.opendocument.database A31536000
ExpiresByType application/vnd.oasis.opendocument.chart A31536000
ExpiresByType application/vnd.oasis.opendocument.formula A31536000
ExpiresByType application/vnd.oasis.opendocument.graphics A31536000
ExpiresByType application/vnd.oasis.opendocument.presentation A31536000
ExpiresByType application/vnd.oasis.opendocument.spreadsheet A31536000
ExpiresByType application/vnd.oasis.opendocument.text A31536000
ExpiresByType audio/ogg A31536000
ExpiresByType application/pdf A31536000
ExpiresByType image/png A31536000
ExpiresByType application/vnd.ms-powerpoint A31536000
ExpiresByType audio/x-realaudio A31536000
ExpiresByType image/svg+xml A31536000
ExpiresByType application/x-shockwave-flash A31536000
ExpiresByType application/x-tar A31536000
ExpiresByType image/tiff A31536000
ExpiresByType application/x-font-ttf A31536000
ExpiresByType application/vnd.ms-opentype A31536000
ExpiresByType audio/wav A31536000
ExpiresByType audio/wma A31536000
ExpiresByType application/vnd.ms-write A31536000
ExpiresByType application/font-woff A31536000
ExpiresByType application/font-woff2 A31536000
ExpiresByType application/vnd.ms-excel A31536000
ExpiresByType application/zip A31536000
</IfModule>
<IfModule mod_deflate.c>
<IfModule mod_setenvif.c>
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
</IfModule>
AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/bmp application/java application/msword application/vnd.ms-fontobject application/x-msdownload image/x-icon image/webp application/json application/vnd.ms-access application/vnd.ms-project application/x-font-otf application/vnd.ms-opentype application/vnd.oasis.opendocument.database application/vnd.oasis.opendocument.chart application/vnd.oasis.opendocument.formula application/vnd.oasis.opendocument.graphics application/vnd.oasis.opendocument.presentation application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.text audio/ogg application/pdf application/vnd.ms-powerpoint image/svg+xml application/x-shockwave-flash image/tiff application/x-font-ttf application/vnd.ms-opentype audio/wav application/vnd.ms-write application/font-woff application/font-woff2 application/vnd.ms-excel
<IfModule mod_mime.c>
# DEFLATE by extension
AddOutputFilter DEFLATE js css htm html xml
</IfModule>
</IfModule>
<FilesMatch "\.(css|htc|less|js|js2|js3|js4|CSS|HTC|LESS|JS|JS2|JS3|JS4)$">
FileETag MTime Size
<IfModule mod_headers.c>
Header set Pragma "public"
Header append Cache-Control "public"
Header unset Set-Cookie
</IfModule>
</FilesMatch>
<FilesMatch "\.(html|htm|rtf|rtx|svg|txt|xsd|xsl|xml|HTML|HTM|RTF|RTX|SVG|TXT|XSD|XSL|XML)$">
FileETag MTime Size
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
Header set Pragma "public"
Header append Cache-Control "public"
</IfModule>
</FilesMatch>
<FilesMatch "\.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|webp|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|_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|_ttf|wav|wma|wri|woff|woff2|xla|xls|xlsx|xlt|xlw|zip|ASF|ASX|WAX|WMV|WMX|AVI|BMP|CLASS|DIVX|DOC|DOCX|EOT|EXE|GIF|GZ|GZIP|ICO|JPG|JPEG|JPE|WEBP|JSON|MDB|MID|MIDI|MOV|QT|MP3|M4A|MP4|M4V|MPEG|MPG|MPE|MPP|OTF|_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|_TTF|WAV|WMA|WRI|WOFF|WOFF2|XLA|XLS|XLSX|XLT|XLW|ZIP)$">
FileETag MTime Size
<IfModule mod_headers.c>
Header set Pragma "public"
Header append Cache-Control "public"
Header unset Set-Cookie
</IfModule>
</FilesMatch>
<FilesMatch "\.(bmp|class|doc|docx|eot|exe|ico|webp|json|mdb|mpp|otf|_otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|pot|pps|ppt|pptx|svg|svgz|swf|tif|tiff|ttf|ttc|_ttf|wav|wri|woff|woff2|xla|xls|xlsx|xlt|xlw|BMP|CLASS|DOC|DOCX|EOT|EXE|ICO|WEBP|JSON|MDB|MPP|OTF|_OTF|ODB|ODC|ODF|ODG|ODP|ODS|ODT|OGG|PDF|POT|PPS|PPT|PPTX|SVG|SVGZ|SWF|TIF|TIFF|TTF|TTC|_TTF|WAV|WRI|WOFF|WOFF2|XLA|XLS|XLSX|XLT|XLW)$">
FileETag None
<IfModule mod_headers.c>
Header unset ETag
Header unset Last-Modified
</IfModule>
</FilesMatch>
<IfModule mod_headers.c>
Header set strict-transport-security "max-age=31536000"
</IfModule>
# END W3TC Browser Cache
The above is this sites entire W3 Total Cache plugins Browser Cache .htaccess rules.
These can be copied in full with no need for any modifications either into your Apache servers httpd.conf file or your domains conf file (if it has one: could be it’s configuration rules are all inside the httpd.conf file).
Whichever Apache conf file you add it to be aware if you also move the main WordPress SEO friendly permalinks rules it probably makes sense to put the Browser Cache rules above the WordPress permalink rules. When I was moving the permalinks rules from the .htaccess file to an Apache conf file I left some of the W3 Total Cache Page Cache rules in the .htaccess file and because they had to load before the permalinks rules it broke the site.
The order rules are loaded is important and the rules inside the httpd.conf file etc… are loaded before any .htaccess file rules.
On my Apache server running Centos 6.9 the file is located at “/etc/httpd/conf.d/httpd.conf”.
I have the “/etc/httpd/conf.d/httpd.conf” file and another conf file “/etc/httpd/conf.d/seo-gold.com.conf” specifically for the SEO Gold domain. My server didn’t by default create the seo-gold.com.conf file, it added all Apache configurations to the main httpd.conf file. I manually created the seo-gold.com.conf file and moved the relevant rules to it.
The server only holds my domains (~100 domains) and I want them all to use the same browser cache rules even though some of the domains don’t use the W3 Total Cache plugin (a few don’t even run WordPress): the plugins browser cache rules are if set correctly suitable for pretty much any website.
To have the browser cache rules work for the entire server (all 100 domains) I simply copy and pasted the entire rules above into the bottom of the “/etc/httpd/conf.d/httpd.conf” file.
After restarting Apache (you must restart Apache for the rules to work) all the domains have the same browser cache options without the need for any plugins or specific rules for each domain.
If all I wanted was these rules to work for the SEO Gold domain I’d find the VirtualHost directives for the SEO Gold domain and add the rules there.
On some servers it will be inside the httpd.conf file, on my server it’s inside the seo-gold.com.conf file and starts like this:
<VirtualHost 166.62.84.189:80>
SuexecUserGroup "#999" "#999"
ServerName seo-gold.com
ServerAlias www.seo-gold.com
etc...
</VirtualHost>
Look for the ServerName example.com line to get you in the right location and find the VirtualHost line above it.
In the example above everything between VirtualHost 166.62.84.189:80 and the next /VirtualHost are the http (unsecure) version directives for the site. Since this site runs under the https (secure) version (has a free Let’s Encrypt SSL certificate) there’s a second set starting VirtualHost 166.62.84.189:443. :80 refers to port 80 (http) and :443 refers to port 443 (https).
I’ve not tested this, but adding the rules to the port: 443 (secure) VirtualHost directives section is all that’s needed.
My file would look something like this:
<VirtualHost 166.62.84.189:443>
SuexecUserGroup "#999" "#999"
ServerName seo-gold.com
ServerAlias www.seo-gold.com
# BEGIN W3TC Browser Cache
Browser cache rules here...
# END W3TC Browser Cache
DocumentRoot /home/domainname/public_html
ErrorLog /var/log/virtualmin/seo-gold.com_error_log
etc...
SSLEngine on
SSLCertificateFile /home/domainname/ssl.cert
SSLCertificateKeyFile /home/domainname/ssl.key
SSLCACertificateFile /home/domainname/ssl.ca
SSLProtocol all -SSLv2 -SSLv3
RemoveHandler .php
RemoveHandler .php5
RemoveHandler .php5.6
php_admin_value engine Off
</VirtualHost>
Take into account this is untested, though where the # BEGIN W3TC Browser Cache rules are added above I have a dozen redirect rules with the format
Redirect permanent /contact-seo-gold.php https://seo-gold.com/hire-a-freelance-seo-consultant/
And they work perfectly.
After copying the W3 Total Cache plugins Browser Cache rules from the root .htaccess file to one of the conf files (as described above: don’t forget to restart Apache) go back to the W3 Total Cache plugin General Settings page and turn the Browser Cache off (it’s no longer required):
Go to “Performance” > “General Settings” : “Browser Cache” Enable Box NOT Ticked.
By unticking the Browser Cache enable box and saving the settings the plugin removes all the “Browser Cache” rules (everything between # BEGIN W3TC Browser Cache and # END W3TC Browser Cache is removed from the .htaccess file).
Since the Browser Cache rules are now active via an Apache server conf file they still work as before.
With the Browser Cache options I used I only noticed one minor change to the remaining rules inside the root .htaccess file and it didn’t appear to make any difference. In effect the W3 Total Cache Browser Cache rules are working without the need for the plugin that created them.
The W3 Total Cache plugin does much more than Browser Caching, so too soon to delete the plugin :-).
David Law