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.
Advertisement
Blogger user here. Thinking of switching to WP soon. This might help.
Great tip, improved my load times tremendously, thanks
I agree, the difference is noticeable. The devs should consider making this code the default rewrite code.
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.
Currently I am using Blogger. When I switch to WP I will try this hack. Thanks 4 sharing.
Excellent tip! thanks for sharing
How would this need to change for a WPMU site?
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.
Wow…..really simple yet powerful tweak. Thanks a lot Martin!
That all works fine, but how about WPMU?
As an alternative we use nginx – so will need to try it there as well.
Thanks for the code. Keep up the good work, you guys are very helpful.
Thanks for this tip. I am sure this will certainly speed-up my fresh blog Techtrickz.
I just added it to my blog as well. Plan on monitoring this a while to see how if it has improved
good start in 2010.
It’s pretty cool hack :) I using on my blog (Modeliuotojas.lt). Thanks ;)