• The rewrite rules used by WP Super Cache will run an order of magnitude faster by using sane syntax. Consider, for example, these two questions:

    Question 1:

    Does your name an an “a” in it?

    Question 2:

    Does your name have the following:
    Starting at the very beginning, some letters or symbols, or maybe none, followed by an “a”, followed by some letters or symbols, or not, all the way to the very end?

    The two questions are actually exactly the same. The second form just has a lot lot of extra words, requiring a lot more mental processing. WP Super Cache uses the second form, requiring a lot more CPU processing.

    For example, this line:
    RewriteCond %{REQUEST_URI} !^.*[^/]$

    The pattern means:
    If it is not true that, at the very beginning, there are some characters, or not, then any character that’s not a slash, at the very end.

    In other words, “if it ends with a slash” Which can much more more succinctly and efficiently written as:
    RewriteCond %{REQUEST_URI} /$

    These two mean exactly the same thing:
    RewriteCond %{REQUEST_URI} !^.*[^/]$
    and
    RewriteCond %{REQUEST_URI} /$

    It probably makes sense to get rid of the double negative and use the second, far simpler, way of saying it.

    Most of the lines have two pieces of silliness:
    RewriteCond %{QUERY_STRING} !^.*=.*$

    ^.* means “if the beginning has some characters, or not”. Since that’s always true, checking for it is just a waste of time.

    Similarly, .*$ means “if there’s anything at the end, or not”, which is again meaningless, it’s always true.

    So this:
    RewriteCond %{QUERY_STRING} !^.*=.*$
    becomes:
    RewriteCond %{QUERY_STRING} !=

    I’m guessing someone read our regex documentation where we say that an anchored pattern can be more efficient. That means a pattern that actually IS anchored. When you know you’re looking for “obama”, not for “dobama”, using the anchor means the finite state machine can stop after the first character if the first character isn’t “o”. A fake anchor like “^.*” doesn’t let it stop, though, it just creates a lot more states to keep track of.

    On another note, there’s no need to process the same set of conditions four times. The common conditions could come first, with a skip rule:
    RewriteRule . – [S=4]

    That will use or skip the four supercache rules with ONE checks of the long patterns, rather than repeating the same comparisons four times. Since it’s a skip, you would of course remove the negation (!) from the beginning of each rule and terminate them with the [OR] flag.

    Here a complete set with the double negatives and fake anchors removed. Maybe later I’ll post a skip set:

    # BEGIN WPSuperCache
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    #If you serve pages from behind a proxy you may want to change 'RewriteCond %{HTTPS} on' to something more sensible
    AddDefaultCharset UTF-8
    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 %{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|Android|BlackBerry9530|LG-TU915\ Obigo|LGE\ VX|webOS|Nokia5800).* [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]
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteCond %{HTTPS} on
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{SERVER_NAME}/$1/index-https.html.gz -f
    RewriteRule ^(.*) "/wp-content/cache/supercache/%{SERVER_NAME}/$1/index-https.html.gz" [L]
    
    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 %{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|Android|BlackBerry9530|LG-TU915\ Obigo|LGE\ VX|webOS|Nokia5800).* [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]
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteCond %{HTTPS} !on
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{SERVER_NAME}/$1/index.html.gz -f
    RewriteRule ^(.*) "/wp-content/cache/supercache/%{SERVER_NAME}/$1/index.html.gz" [L]
    
    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 %{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|Android|BlackBerry9530|LG-TU915\ Obigo|LGE\ VX|webOS|Nokia5800).* [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]
    RewriteCond %{HTTPS} on
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{SERVER_NAME}/$1/index-https.html -f
    RewriteRule ^(.*) "/wp-content/cache/supercache/%{SERVER_NAME}/$1/index-https.html" [L]
    
    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 %{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|Android|BlackBerry9530|LG-TU915\ Obigo|LGE\ VX|webOS|Nokia5800).* [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]
    RewriteCond %{HTTPS} !on
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{SERVER_NAME}/$1/index.html -f
    RewriteRule ^(.*) "/wp-content/cache/supercache/%{SERVER_NAME}/$1/index.html" [L]
    </IfModule>
    
    # END WPSuperCache

    https://www.ads-software.com/extend/plugins/wp-super-cache/

Viewing 6 replies - 16 through 21 (of 21 total)
  • Yes, supercache by default requires a slash at the end. I ran into that the other day when I was working on caching searches (a spammer/search engine almost took down my site by running lots of searches the other day), and so I debated between changing the URLs (though search engines have non-trailing slash URLs that they currently use), or changing my supercache rules to not need a slash.

    I ended up doing both: removing the slash URLs, and redirecting searches to specific formats that I want them to use.

    As for the GET URLs, I understand that you want to differentiate pages depending on the GET urls, so that means mod_rewrite can’t cache those pages at all. As far as I can tell the %{QUERY_STRING} != rule works fine, presumably meaning “not blank”, and so that should work for you too, where mod_rewrite skips the cache, and then php takes over to redirect/cache/whatever it wants to do.

    I’m not sure what you mean by “second cache”, once you’ve entered the PHP world. And as far as I can tell, your GET URLs won’t be mod_rewrite cached at all.

    @jondaley: let’s move on from the GET caching, the bottom line is we want mod_rewrite to work better – not stop /?p= being redirected to its permalink.

    As far as I can see, there are three rewrite conditions that are suspect and should probably be optimized. These are:

    RewriteCond %{REQUEST_URI} !^.*[^/]$
    RewriteCond %{REQUEST_URI} !^.*//.*$
    RewriteCond %{QUERY_STRING} !.*=.*

    So what I’m trying to understand is what does each check?

    1) Checks if the URI ends with a slash? If so, then surely */$ is enough?

    2) Checks if the URI ends with double-slash? What for and why for? And if that’s needed, then surely !*//$ is enough?

    3) Checks if there is a query string? Or if the query string is empty? I don’t get that one

    I’m also curious what’s HTTP:profile — I could not find anything about it anywhere.

    I don’t know why there is a check for a double slash.

    I understand why someone might want a check for an ending slash, but I haven’t found it useful. I think what that rule wants to say is “end with a slash, or be blank”, so that is probably best written as (|/)$ (I didn’t test that, because again, I don’t find that rule useful) I think raymor’s analysis of ![^/]$ is fine too, I don’t know performance-wise if one is better than the other.

    Yes, I don’t know what the HTTP:Profile is, I removed it.

    For the query string, as far as I can tell, it just saying QUERY_STRING contains an equal sign, and so is a long way of saying !=, and so the new rule is better than the original, but I think you are saying that you see a difference in the two rules?

    Interesting stuff. I wish Donncha would join in and tell us his reasoning, it would help ??

    I will run some testing later tonight on different options for these two rules and will write back here later.

    HTTP:profile, x-wap-profile — these are imho redundant. The first I couldn’t find anything about; the second is relevant for WAP phones. Unless your website has a WAP version, I don’t get the point of it. If someone accesses my site with a WAP browser, if he’s even able to load it, then why shouldn’t it be cached ??

    Yes, I do wonder where Donncha is – hopefully he’s just on vacation, and hasn’t disappeared entirely.

    I think a bunch of these rules are fine, but should be tied to the checkboxes in the admin. I suspect that the admin interface is more tied to php/legacy modes, rather than mod_rewrite.

    I’d love to see some of this for nginx.

Viewing 6 replies - 16 through 21 (of 21 total)
  • The topic ‘Supercache can be ten times faster by using sane rewrite syntax’ is closed to new replies.