Speed Up Wordpress With Better Permalink Rewrite Code - gHacks Tech News

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."

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.

We need your help

Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.

If you like our content, and would like to help, please consider making a contribution:

Comments

  1. Lapinas said on January 9, 2010 at 1:11 pm
    Reply

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

  2. imagingdesk said on January 9, 2010 at 4:03 pm
    Reply

    good start in 2010.

  3. Ajay D'Souza said on January 9, 2010 at 9:03 pm
    Reply

    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] said on January 10, 2010 at 4:48 am
    Reply

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

  5. Computer Repair said on January 10, 2010 at 4:59 am
    Reply

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

  6. Brands-and-Jingles said on January 10, 2010 at 4:42 pm
    Reply

    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 said on January 10, 2010 at 5:37 pm
    Reply

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

  8. Dave said on January 12, 2010 at 6:08 am
    Reply

    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 said on March 4, 2010 at 1:06 am
    Reply

    How would this need to change for a WPMU site?

  10. Omer Greenwald said on March 16, 2010 at 8:23 pm
    Reply

    Excellent tip! thanks for sharing

  11. Amith said on November 29, 2010 at 5:04 pm
    Reply

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

  12. tom said on January 21, 2011 at 11:21 am
    Reply

    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 said on April 22, 2011 at 8:01 pm
    Reply

    Great tip, improved my load times tremendously, thanks

    1. Martin Brinkmann said on April 22, 2011 at 8:31 pm
      Reply

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

  14. Shanjei said on February 14, 2017 at 1:23 pm
    Reply

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

Leave a Reply