773-809-5002

Free Website Evaluation

Speed Up the Rewrite Engine for WordPress

While reading Michael Gray’s post on WordPress’ scalability, I followed this link to a webmasterworld.com forum on WordPress’ default .htaccess code. jdMorgan replied to thread with a whole new set of code to replace the .htaccess file that WordPress installs to optimize the URLs in a blog. jdMorgan had rewritten the .htaccess code in order to,

…fix several performance-affecting problems. Notably, the unnecessary and potentially-problematic <IfModule> container is completely removed, and code is added and re-structured to both prevent completely-unnecessary file- and directory- exists checks and to reduce the number of necessary -exists checks to one-half the original count (due to the way mod_rewrite behaves recursively in .htaccess context).

Without further ado, here is jdMorgan’s new code to completely replace what WordPress automatically installs:

[EDIT] Several people commented that they were getting 500 errors. Thanks to Wayne Cochran for pointing out that the RewriteBase / needed to be uncommented for many/most people. I have edited the code below to remove the comment pound sign.

# BEGIN WordPress
#
RewriteEngine on
#
# Unless you have set a different RewriteBase preceding this point,
# you may delete or comment-out the following RewriteBase directive
RewriteBase /
#
# if this request is for “/” or has already been rewritten to WP
RewriteCond $1 ^(index\.php)?$ [OR]
# or if request is for image, css, or js file
RewriteCond $1 \.(gif|jpg|ico|css|js)$ [NC,OR]
# or if URL resolves to existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# or if URL resolves to existing directory
RewriteCond %{REQUEST_FILENAME} -d
# then skip the rewrite to WP
RewriteRule ^(.*)$ – [S=1]
# else rewrite the request to WP
RewriteRule . /index.php [L]
#
# END wordpress

Our Unofficial Results

I’ve been a bit concerned with how quickly a couple of our WordPress installs have been loading, so I was anxious to give this a try. I did some testing on two sites as they are now. Most of the pages were taking about three seconds to load (yes, I took the cache out of the equation). I installed the new code and tried again (cache still turned off) and the pages were installing in somewhere around two seconds. About a 50% increase in load speeds. This is all, of course, very unscientific, but that much of a change was enough for me to write this post.

Of course, your milage may vary! It might even crash your website for all I know?I’m certainly not guaranteeing jdMorgan’s code. All I know is that it seems to have sped up a couple of our WordPress-powered blogs.

How to Change the .htaccess File

If none of the above makes much sense to you, but you want to try to speed up your WordPress blog…here’s what you need to do. You will need an FTP client, FTP access to your WordPress installation, and a text editor. If you aren’t sure if you have these things…it’s time to write to someone who can help you.
  1. Log in to your WordPress install via your FTP client.
  2. Navigate to the root of the install. This usually means inside the “public” folder.
  3. The .htaccess file is an “invisible” file, meaning that unless you work at it, you won’t see the file listed. To see the file, make sure your FTP client has “View Invisibles” turned on. This varies from client to client, but often there is a “View” pulldown menu item that will let you toggle this setting.
  4. Once you can see the .htaccess file, open it up with your text editor. (Depending on your set up, you may have to download it first and then edit it.)
  5. Delete the WordPress generated rewrite code, which is everything from #Begin WordPress to #End WordPress.
  6. Replace it with the code above.
  7. Save the changes. (If you downloaded the file to edit it, remember to upload it and overwrite the old file.)

I hope you have as much luck with this as we did.

Speed is Officially a Factor in Google Rankings

It was recently announced that one of the new factors Google takes into consideration for ranking a website is how quickly the pages load. With this in mind, these types of tweaks can have more impact than just making your readers a bit happier. Take a look at our complete guide to speeding up a website for an expanding list of tips to optimize your load time.

About Rod Holmes

Rod is a partner at Chicago Style SEO, a full-service Internet marketing company in Chicago. His main responsibility around the office is being the head PPC wrangler (he's a certified Google AdWords expert). He also seems to spend a lot of time on the phone talking with clients and prospective clients.You can find Rod on several social media channels: , Twitter, and Facebook.

6 Responses to Speed Up the Rewrite Engine for WordPress

  1. AskApache April 22, 2010 at 2:35 pm #

    Holy smokes Batman! That is freaking AWESOME. I can’t believe I never thought to do that, but its no surprise that it was jdMorgan’s idea.. He’s literally the guy I goto when I am stumped on htaccess.

    +1 for you for recognizing the value of this trick, literally 50% would be an under-exaggeration on a super optimized blog. I mean this comes into play for 90% of every http packet sent! Thanks a lot for sharing..

  2. reader September 29, 2012 at 1:09 pm #

    This gave me a 500 internal error just FYI… cheers -

    • Rod Holmes October 1, 2012 at 4:13 am #

      It may be that your hosting company’s version of Apache isn’t supporting some aspect of the rewrite. We just used this last week on a website we are working on, so I know that it does still work.

  3. Wayne Cochran December 18, 2012 at 7:41 am #

    The code above generated a 500 error for me, however, the code below worked for me:

    [code]
    # BEGIN WordPress
    RewriteEngine on
    #
    # Unless you have set a different RewriteBase preceding this
    # point, you may delete or comment-out the following
    # RewriteBase directive:
    RewriteBase /
    #
    # if this request is for "/" or has already been rewritten to WP
    RewriteCond $1 ^(index\.php)?$ [OR]
    # or if request is for image, css, or js file
    RewriteCond $1 \.(gif|jpg|css|js|ico)$ [NC,OR]
    # or if URL resolves to existing file
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    # or if URL resolves to existing directory
    RewriteCond %{REQUEST_FILENAME} -d
    # then skip the rewrite to WP
    RewriteRule ^(.*)$ - [S=1]
    # else rewrite the request to WP
    RewriteRule . /index.php [L]
    #
    # END WordPress
    [/code]

    • Rod Holmes December 18, 2012 at 7:54 am #

      Thanks Wayne, I have made changes to the main code above to remove the comment on the RewriteBase / line.

Trackbacks/Pingbacks

  1. Google’s Need for Speed – Use Cache in .htaccess to Speed Up Your Site — Chicago Style SEO - April 21, 2010

    [...] in .htaccess already, you might want to check out a post I wrote a while back about how to speed up the WordPress rewrite engine for optimized URLs. If you’re using WordPress and are using optimized (readable) URLs, this [...]

Leave a Reply