Skip to main content

Speed Up WordPress Load Time

Published on
I like that WordPress has a built in permalink builder, however I wish it had a built in way of adding your own custom rules. You can, however, write a function to hook with the flush_rewrite_rules() built in WordPress. I borrowed some of Sergey Chernyshev's .htaccess lines and created a function to add gzip compression and caching rules to speed up WordPress load time. My Google PageSpeed analysis went up about 11 points after I applied the hitaccess rules. Speed up WordPress load time by adding this bit of code into your functions.php. You'll still need to flush the rewrite rules, so after you've saved your functions.php go to Settings -> Permalinks and click on Save Settings. For those who want proof, here is a screenshot of my page speed results glamanate-page-speed
/** Functions to add to htaccess through WordPress**/
add_action('mod_rewrite_rules', 'dooley_htaccess_rules');
function dooley_htaccess_rules( $rules ) {

//echo'<pre>'.$rules.'</pre>';

$custom_rules = 
"\n # BEGIN Dooley Rules -  See https://github.com/sergeychernyshev/.htaccess
 \n# --------------------------------------------------------------------------------------
 \n# Compression: http://code.google.com/speed/page-speed/docs/payload.html#GzipCompression
 \n# --------------------------------------------------------------------------------------
 \n<IfModule mod_deflate.c>
 \nAddOutputFilter DEFLATE application/atom+xml
 \nAddOutputFilter DEFLATE application/json
 \nAddOutputFilter DEFLATE application/xhtml+xml
 \nAddOutputFilter DEFLATE application/xml
 \nAddOutputFilter DEFLATE text/css
 \nAddOutputFilter DEFLATE text/html
 \nAddOutputFilter DEFLATE text/plain
 \nAddOutputFilter DEFLATE text/x-component
 \nAddOutputFilter DEFLATE text/xml
 \n
 \n# The following MIME types are in the process of registration
 \nAddOutputFilter DEFLATE application/xslt+xml
 \nAddOutputFilter DEFLATE image/svg+xml
 \n
 \n# The following MIME types are NOT registered
 \nAddOutputFilter DEFLATE application/mathml+xml
 \nAddOutputFilter DEFLATE application/rss+xml

 \n# JavaScript has various MIME types
 \nAddOutputFilter DEFLATE application/javascript
 \nAddOutputFilter DEFLATE application/x-javascript
 \nAddOutputFilter DEFLATE text/ecmascript
 \nAddOutputFilter DEFLATE text/javascript
 \n
 \n# .ico files and other compressible images
 \nAddOutputFilter DEFLATE image/vnd.microsoft.icon
 \nAddOutputFilter DEFLATE image/x-icon
 \nAddOutputFilter DEFLATE image/bmp
 \nAddOutputFilter DEFLATE image/tiff
 \nAddOutputFilter DEFLATE application/pdf
 \n
 \n# compressible fonts (.woff is already compressed)
 \nAddOutputFilter DEFLATE font/opentype
 \nAddOutputFilter DEFLATE application/x-font-ttf
 \nAddOutputFilter DEFLATE application/vnd.ms-fontobject
 \n</IfModule>
 \n# -------------------------------------------------------------------------------------------------
 \n# Browser Caching: http://code.google.com/speed/page-speed/docs/caching.html#LeverageBrowserCaching
 \n# --------------------------------------------------------------------------------------------------
 \n<IfModule mod_expires.c>
 \nExpiresActive On
 \n
 \nExpiresByType application/json\"access plus 1 year\"
 \nExpiresByType application/pdf\"access plus 1 year\"
 \nExpiresByType application/x-shockwave-flash\"access plus 1 year\"
 \nExpiresByType image/bmp \"access plus 1 year\"
 \nExpiresByType image/gif \"access plus 1 year\"
 \nExpiresByType image/jpeg \"access plus 1 year\"
 \nExpiresByType image/png \"access plus 1 year\"
 \nExpiresByType image/svg+xml \"access plus 1 year\"
 \nExpiresByType image/tiff \"access plus 1 year\"
 \nExpiresByType image/vnd.microsoft.icon \"access plus 1 year\"
 \n  ExpiresByType image/x-icon\"access plus 1 year\"
 \nExpiresByType text/css \"access plus 1 year\"
 \nExpiresByType video/x-flv \"access plus 1 year\"
 \nExpiresByType application/vnd.bw-fontobject\"access plus 1 year\"
 \nExpiresByType application/x-font-ttf\"access plus 1 year\"
 \nExpiresByType application/font-woff\"access plus 1 year\"
 \nExpiresByType font/opentype\"access plus 1 year\"
 \nExpiresByType image/webp\"access plus 1 year\"
 \n
 \n# The following MIME types are in the process of registration
 \nExpiresByType application/xslt+xml\"access plus 1 year\"
 \nExpiresByType image/svg+xml\"access plus 1 year\"
 \n
 \n# The following MIME types are NOT registered
 \nExpiresByType application/mathml+xml\"access plus 1 year\"
 \nExpiresByType application/rss+xml\"access plus 1 year\"
 \n
 \n# JavaScript has various MIME types
 \nExpiresByType application/x-javascript \"access plus 1 year\"
 \nExpiresByType application/javascript \"access plus 1 year\"
 \nExpiresByType text/ecmascript \"access plus 1 year\"
 \nExpiresByType text/javascript \"access plus 1 year\"
 \n</IfModule>

 \n# END Dooley Rules\n";

 echo $custom_rules;

return $custom_rules . $rules;

}
There you go.