Speed Up WordPress With Better Permalink Rewrite Code

WordPress by default uses a link scheme for articles that uses numbers instead of words, and it is considered bad not only for the user experience but also from a SEO perspective.

The permalink setting, which defines these urls the blog creates, need to be changed in the administrative dashboard, and it is up to the blog administrator to pick a different scheme.

It is for instance possible to use urls that display the category and page title as the url, to mix in dates, numbers or even custom information.

The most important rule for search engine optimization is to use the page title in the url. Everything else is optional and up to the user's liking.

WordPress generates an .htaccess file when the permalinks are changed that contains the rewrite directives. It sometimes happens that the file cannot be created or modified, for instance if it is locked down on the web server. If that is the case, it is necessary to create or edit the htaccess file manually on the server before the changes take effect.

The WordPress code that is used for that is not optimized. It does not prevent for instance unnecessary file and directory checks. JP Morgan over at the Webmaster World forum created a better rewrite directive that "fixes several performance-affecting problems".

This is a total replacement for the code supplied with WP as bounded by the "Begin WP" and "End WP" comments, and fixes several performance-affecting problems. Notably, the unnecessary and potentially-problematic 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).

According to JP the modified code speeds up the .htaccess code by at least a factor of two by "avoiding the second-pass exists checks on index.php itself, and avoiding exists-checks on resources such as image files which obviously don't need to be handled by WP."

Read also:  Windows 10 Safe Mode

Replace the old WordPress rewrite code in the .htaccess file with the following new code. You might need to edit the file types (gif|jpg|php|ico|css|js). These should contain the files that are requested the most. It might make sense to include png for instance in the list.

# 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|php|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

This change seems to speed up the WordPress loading time considerably. Let us know how you think it affected the page loading time if you have implemented it in your blog or noticed a difference here at Ghacks.

Visit the thread over at the Webmaster World forum for additional information.

Advertisement
Please share this article

Facebooktwittergoogle_plusredditlinkedinmail



Responses to Speed Up WordPress With Better Permalink Rewrite Code

  1. Lapinas January 9, 2010 at 1:11 pm #

    It's pretty cool hack :) I using on my blog (Modeliuotojas.lt). Thanks ;)

  2. imagingdesk January 9, 2010 at 4:03 pm #

    good start in 2010.

  3. Ajay D'Souza January 9, 2010 at 9:03 pm #

    I just added it to my blog as well. Plan on monitoring this a while to see how if it has improved

  4. [email protected] January 10, 2010 at 4:48 am #

    Thanks for this tip. I am sure this will certainly speed-up my fresh blog Techtrickz.

  5. Computer Repair January 10, 2010 at 4:59 am #

    Thanks for the code. Keep up the good work, you guys are very helpful.

  6. Brands-and-Jingles January 10, 2010 at 4:42 pm #

    That all works fine, but how about WPMU?

    As an alternative we use nginx - so will need to try it there as well.

  7. Indrek January 10, 2010 at 5:37 pm #

    Wow.....really simple yet powerful tweak. Thanks a lot Martin!

  8. Dave January 12, 2010 at 6:08 am #

    That's very interesting... I have never heard anyone mention optimizing the .htaccess file to improve performance, but it makes perfect sense.

    However, I must admit I am kind of afraid of making the changes, since I don't fully understand .htaccess, and if my permalink structure somehow changed I could lose my Google PR which took a ton of work to obtain.

  9. Tom Altman March 4, 2010 at 1:06 am #

    How would this need to change for a WPMU site?

  10. Omer Greenwald March 16, 2010 at 8:23 pm #

    Excellent tip! thanks for sharing

  11. Amith November 29, 2010 at 5:04 pm #

    Currently I am using Blogger. When I switch to WP I will try this hack. Thanks 4 sharing.

  12. tom January 21, 2011 at 11:21 am #

    This seems to give a problem with the wp e-commerce plugin. When you trie to add a product to the shoppingcart, the link doesn't work properly.

  13. Troy Janda April 22, 2011 at 8:01 pm #

    Great tip, improved my load times tremendously, thanks

    • Martin Brinkmann April 22, 2011 at 8:31 pm #

      I agree, the difference is noticeable. The devs should consider making this code the default rewrite code.

  14. Shanjei February 14, 2017 at 1:23 pm #

    Blogger user here. Thinking of switching to WP soon. This might help.

Leave a Reply