Automattic / Automattic/wp-super-cache
Compressed and mobile optimized .htaccess rules for Apache
- Dominant language
- PHP
- Stars
- 436
- Forks
- 130
- Avg merge
- 15h 11m
- Merged PRs (30d)
- 10
Description
Hello,
I'd like to discuss with you this first version of optimizations and compression of the .htaccess rules for Apache I made these days in order to serve more efficiently supercache static files without having to summon PHP. I'm struggling with RAM consumption when traffic peaks so I'm trying to reduce Apache memory consumption and time spent per process the best I can.
We all know how standard WPSC .htaccess rules look like, the four big blocks, so I'm not repeating them here. Four is because they must check for https and gzip in a redundat way.
System requirements for my suggested compressed rules are:
- Apache's [mod_env](https://httpd.apache.org/docs/2.4/mod/mod_env.html) to be enabled on the server. Used to emulate string variables.
The rules are:
```apache
# add support per mobile supercache
# works until wp_cache_mobile_group() returns always 'mobile'
# or you have to customize based on $wp_cache_mobile_groups
RewriteRule ^ - [E=wpsc_mobile]
RewriteCond %{HTTP_USER_AGENT} ^.*(2.0\ MMP|240x320|400X240|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|Googlebot-Mobile|hiptop|IEMobile|KYOCERA/WX310K|LG/U990|MIDP-2.|MMEF20|MOT-V|NetFront|Newt|Nintendo\ Wii|Nitro|Nokia|Opera\ Mini|Palm|PlayStation\ Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|SHG-i900|Small|SonyEricsson|Symbian\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|webOS|Windows\ CE|WinWAP|YahooSeeker/M1A1-R2D2|iPhone|iPod|iPad|Android|BlackBerry9530|LG-TU915\ Obigo|LGE\ VX|webOS|Nokia5800).* [OR,NC]
RewriteCond %{HTTP_USER_AGENT} ^(w3c\ |w3c-|acs-|alav|alca|amoi|audi|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-|dang|doco|eric|hipt|htc_|inno|ipaq|ipod|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-|lg/u|maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|palm|pana|pant|phil|play|port|prox|qwap|sage|sams|sany|sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo|teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|wap-|wapa|wapi|wapp|wapr|webc|winw|winw|xda\ |xda-).* [NC]
RewriteRule ^ - [E=wpsc_mobile:-mobile]
# https detection
RewriteRule ^ - [E=wpsc_https]
RewriteCond %{HTTPS} on
RewriteRule ^ - [E=wpsc_https:-https]
# gzip detection
RewriteRule ^ - [E=wpsc_gzip]
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteRule ^ - [E=wpsc_gzip:.gz]
# single block instead of the four blocks
RewriteCond %{REQUEST_URI} !^.*[^/]$
RewriteCond %{REQUEST_URI} !^.*//.*$
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$
RewriteCond %{HTTP:X-Wap-Profile} !^[a-z0-9\"]+ [NC]
RewriteCond %{HTTP:Profile} !^[a-z0-9\"]+ [NC]
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{SERVER_NAME}/$1/index%{ENV:wpsc_https}%{ENV:wpsc_mobile}.html%{ENV:wpsc_gzip} -f
RewriteRule ^(.*) "/wp-content/cache/supercache/%{SERVER_NAME}/$1/index%{ENV:wpsc_https}%{ENV:wpsc_mobile}.html%{ENV:wpsc_gzip}" [L]
```
For my website configuration they are enough in that form.
How they work is quite simple:
In the first block I'm detecting mobile by searching in the HTTP_USER_AGENT. If something is detected I save "-mobile" in the env variable named **wpsc_mobile**, otherwise it remains empty "". I then use the variable in the long file name at the bottom. This optimization works in my system since I don't have any other mobile groups defined so I pretty much only support the "-mobile" suffix, tell me if I'm wrong here.
Same goes with the en variables **wpsc_https** and **wpsc_gzip**. They'll get expanded to "-https" and ".gz" when https and gzip support are detected.
Does this make sense? It's working for me right now, so I hope yes. Maybe it could be implemented somehow in the official WPSC in the future? Problem is I don't know if mod_env get enabled by default on most shared servers, mine is a dedicated so I didn't have problem with it.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.