WordPress rewrite rules

Friday, 8 July 2005

This is an investigation into some problems with the way WordPress generates .htaccess rules to make its fancy permalinks work. For a new site I am building, I wanted the permalinks to not have the date in the URL. Accordingly, I set the permalink structure to the following.

/%category%/%postname%/

Once I did this, I couldn’t access any single posts. The Codex warns about related issues:

Note on using only %postname%
If you use postname as the only element in your permalinks to create a structure such as myblog.com/post-title, the rewrite rules may make it impossible to access pages such as your stylesheet (which has a similar format) or the wp-admin folder. It’s best to include some numeric data (e.g. the post ID or date) in the permalink to prevent this from happening.

Note on using %category%
%category% does not work correctly with mod_rewrite in Apache versions prior to 2. If you are using Apache 1, do not use %category% in your permalink structure.

Hmmm. My server uses Apache 1, and I had no problems with %category%, but maybe this is related to my problem. The next step was to look inside the .htaccess file to see what was going on. Here are the last few rules WordPress generated.

RewriteRule ^(.+)/feed/(feed|rdf|rss|rss2|atom)/?$ /blog/index.php?category_name=$1&feed=$2 [QSA,L]
RewriteRule ^(.+)/(feed|rdf|rss|rss2|atom)/?$ /blog/index.php?category_name=$1&feed=$2 [QSA,L]
RewriteRule ^(.+)/page/?([0-9]{1,})/?$ /blog/index.php?category_name=$1&paged=$2 [QSA,L]
RewriteRule ^(.+)/?$ /blog/index.php?category_name=$1 [QSA,L]
RewriteRule ^(.+)/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ /blog/index.php?category_name=$1&name=$2&feed=$3 [QSA,L]
RewriteRule ^(.+)/([^/]+)/(feed|rdf|rss|rss2|atom)/?$ /blog/index.php?category_name=$1&name=$2&feed=$3 [QSA,L]
RewriteRule ^(.+)/([^/]+)/page/?([0-9]{1,})/?$ /blog/index.php?category_name=$1&name=$2&paged=$3 [QSA,L]
RewriteRule ^(.+)/([^/]+)(/[0-9]+)?/?$ /blog/index.php?category_name=$1&name=$2&page=$3 [QSA,L]
RewriteRule ^(.+)/([^/]+)/trackback/?$ /blog/index.php?category_name=$1&name=$2&tb=1 [QSA,L]

The first four rules in this excerpt deal with category requests, and the rest deal with single-post requests. Note that the fourth rule in this excerpt matches everything, so the subsequent single-post rules are never used. This explained the problem.

The solution is simple: just move the four category rules to the end of the block. That fixes the problem, but it means I will have to manually fix the file every time WordPress regenerates the rewrite rules. This happens every time you create or edit a page, and maybe at other times too. What a pain.

Mysteriously, this problem did not happen on my test server. There, WordPress put the four category rules after the single-post rules automatically, so there were no problems. There are many differences between my test and live servers, so I’ll need to figure out which differences are significant.

My next step will be to look into how WordPress generates its rules. It would be nice to remove any restrictions on permalink structures.

Tags:

3 comments

You can leave a comment, or trackback from your own site.

  1. I’m having the same problem. What do you mean by “to the end of the block”?

    TIA

  2. Never mind I got it working!! After reading your post a few more time (duh) LOL
    Thank you much for posting this

Leave a comment