Our ecommerce system has about a 300 - 500 ms TTFB, which I don't think is bad at all, but still isn't as good as a static file. This week I finally experimented with creating my own local cache of product / category pages, and telling our server to rewrite to those files if available, else rewrite to the ecommerce system.
It works great and our TTFBs are usually around 50 - 100 ms now. I also watched our server load average in top and, for the most part, the numbers went from around 0.5 down to 0.2ish (with some occasional jumps due to traffic). So in general it looks like this is making the server run smoother as well as well as speed up page load time, which is what I hoped for.
That being said I'm no expert on rewrites and server performance - can anyone do a quick review and let me know if there's room for improvement? These rewrites are located in the vhost file.
# product pages
RewriteCond %{REQUEST_URI} ^/product_([^.]+).*$
RewriteCond %{DOCUMENT_ROOT}\/\local_http_cache\/product_%1.php -f
RewriteRule (.*) /local_http_cache/product_%1.php?CacheFlag [PT,QSA,L]
RewriteRule ^/product_([^.]+).*$ /path/to/OurEcommerceSystem?ProductID=$1 [PT,QSA]
# category pages
RewriteCond %{REQUEST_URI} ^/category_([^.]+).*$
RewriteCond %{QUERY_STRING} !((^|&)(Sort_By|Per_Page)=(.*)+(&|$))
RewriteCond %{DOCUMENT_ROOT}\/\local_http_cache\/category_%1.php -f
RewriteRule (.*) /local_http_cache/category_%1.php?CacheFlag [PT,QSA,L]
RewriteRule ^/category_([^.]+).*$ /path/to/OurEcommerceSystem?CategoryID=$1 [PT,QSA]
Couple of other notes:
For both product and category pages the main goal is the same: check if a cache file exists for this product / category ID. If so, serve it, else do nothing and let the next rewrite send the request to our ecommerce system.
The
?CacheFlagdoesn't do anything particularly functional - but a rule in/local_http_cache/'s .htaccess file tells it to forbid anything that doesn't have that flag, so no one can browse those files directlythe cache pages are purposely PHP due to some code that checks session IDs, but I'm going to look in to eliminating that, which would allow me to cache just static html code.
RewriteRulefirst and then atRewriteCond. Really neat! I also took your advice and used the env var, and will move that .htaccess rule to vhost as well. For thePTflag, our ecommerce system actually does need it, but the cache I've built doesn't, so I removed it as you suggested. Thank you very much @MrWhite! – Mike Willis Mar 18 '19 at 18:52