Choosing The Right WordPress Permalink Structure

We made a big mistake with our permalink structure when setting up our first wordpress install. Don’t do the same! If not familiar with permalinks the official wordpress documentation can be found here.

Our Mistake

We went with a very logical, visitor-friendly, search engine friendly permalink structure.

/%category%/%postname%/

Little did we know that it would be revealed that this apparently leads to very big performance issues once post numbers start to grow.

“For performance reasons, it is not a good idea to start your permalink structure with the category, tag, author, or postname fields.

The reason is that these are text fields, and using them at the beginning of your permalink structure it takes more time for WordPress to distinguish your Post URLs from Page URLs (which always use the text “page slug” as the URL), and to compensate, WordPress stores a lot of extra information in its database (so much that sites with lots of Pages have experienced difficulties).

So, it is best for the first structure tag to be a numeric one, such as /%year%/%postname%/. (Some people recommend /%post_id%/%postname%/ which works for performance reasons but others recommend against it because it is unfriendly to users in the many contexts in which users interact with URLs.) See Otto’s technical writeup on the topic as well as this wp-testers discussion. “

What WordPress Actually Does

“Given a URL, WordPress first applies the rewrite rules to the URL. The rewrite rules are generated based on several things, and they essentially allow WordPress to take the URL and determine that /2009/02/05/my-post breaks down into year=2009, month=02, etc…

Because WordPress has a built in set of things called “Pages”, these can interfere with the rewrite rules when the first parameter is non-numeric. For the numeric matches like year, WordPress can simply look at it and say “hey, that’s a number, it’s probably not a Page, and hey, it fits in with the %year% he has in the permalink structure, so let’s go with that”. But a Page will have a URL like example.com/blog/pagename. So WordPress can see “hey, that’s not a number, it must be a Page name, let’s skip ahead here”.

So, when you use the four non-numeric items (%category%, %tag%, %postname%, or %author%) first, then it’s hard for WordPress to distinguish between one of those and a Page name immediately. This is why the rewrite_rules option expands. Suddenly, WordPress can’t use the numeric shortcut any more. Now, it has to take that string and compare it against all of the possible Page names and see whether it fits or not. This is a big performance hit, obviously, but it’s also a big list of Pages that it has to generate as well. Internally, this is called “use_verbose_page_rules” and it defaults to off, only getting turned on when there’s no other choice.

Anyway, regardless of what happens, the end result of the rewrite handling is that the URL is broken down into components (year=2009, month=02, etc…), and then this is filtered through to the WP_Query system, which determines how specific of a page it can get. The most specific wins. Meaning that if there is enough information to get a single post, then it gets a single post. If it only has category, then it gets a category archive. If it has year and month, then it gets a monthly archive, and so forth.”

Source: http://dougal.gunters.org/blog/2009/02/04/efficient-wordpress-permalinks/

Fixing The Problem

“Just to reiterate, using one of those ‘bad’ structures is only a problem if your site has many, many posts/pages. And there are plenty of WordPress admins who will tell you that they run high-traffic sites with that same structure, with *lots* of posts, and have no problems at all.

And the use of a caching plugin like WP Super Cache will just about nullify the problem anyways, since it will cache the pages to disk, bypassing the need for the PHP + MySQL processing to look up the page mapping, anyways”

Source: http://dougal.gunters.org/blog/2009/02/04/efficient-wordpress-permalinks/

Just to clarify the real problem is with a huge number of pages.

WP Super Cache

WP Super Cache is a very popular WordPress plugin that loads static HTML pages rather than their dynamic counterparts. WP Super Cache Page.

There is a note that those using the /%category%/%postname%/ permalink structure may encounter errors:

“A category page is showing instead of my homepage

A tiny proportion of websites will have problems with the following configuration:

  1. Uses a static page for the front page.
  2. Uses /%category%/%postname%/ permalink structure.

Sometimes a category page is cached as the homepage of the site instead of the static page. I can’t replicate the problem but a simple solution is to switch the plugin to PHP mode. For normal traffic you will see no difference in the speed of your site. You can also enable “Extra homepage checks” on the Advanced Settings page.”

Source: http://wordpress.org/extend/plugins/wp-super-cache/faq/

More on Permalinks & WordPress

Why wordpress sucks (basically)

(2nd part and comments blast WordPress for its inefficient database queries to serve posts – part of the permalink issue)

Is WordPress good or bad for SEO?

Many SEO experts give the wrong advice regarding WordPress.

This entry was posted in Wordpress and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>