WordPress Rewrite Rules
I upgraded WordPress yesterday and ran into some problems today with the .htaccess rewrite rules, which I’ve just fixed. I’m sharing these rules below. This is for people who are looking to write .htaccess rules for their WordPress setup and need some idea of how to go about doing that.
Note: this is for a custom-structure setting of /%post_id%/. Modify your’s accordingly.
For Months:
RewriteRule ^blog/date/199([0-9]+)/([a-zA-Z0-9]+)$ /blog/?m=199$1$2 [L]
RewriteRule ^blog/date/199([0-9]+)/([a-zA-Z0-9]+)/$ /blog/?m=199$1$2 [L]
RewriteRule ^blog/date/200([0-9]+)/([a-zA-Z0-9]+)$ /blog/?m=200$1$2 [L]
RewriteRule ^blog/date/200([0-9]+)/([a-zA-Z0-9]+)/$ /blog/?m=200$1$2 [L]
These are doubled to account for an absent trailing slash.
For Months that are broken into pages (again doubled for a missing slash, and here I’m not taking into account any postings from the 1990s):
RewriteRule ^blog/date/200([0-9]+)/([a-zA-Z0-9]+)/page/([a-zA-Z0-9]+)/$ /blog/?m=200$1$2&paged=$3 [L]
RewriteRule ^blog/date/200([0-9]+)/([a-zA-Z0-9]+)/page/([a-zA-Z0-9]+)$ /blog/?m=200$1$2&paged=$3 [L]
For basic entries:
RewriteRule ^/blog/?p=([a-zA-Z0-9]+)$ /blog/$1 [L,R=301]
RewriteRule ^blog/([a-zA-Z0-9]+)/$ /blog/?p=$1 [L]
RewriteRule ^blog/([a-zA-Z0-9]+)$ /blog/?p=$1 [L]
The first rule above takes into account anyone’s old links to your pages using the default format. The second rule assumes a trailing slash, and the third rule assumes a forgotten slash.
For pages:
This example uses my About page, which under the default url-setting has an id of 2. Again doubled for slashes:
RewriteRule ^blog/about/$ /blog/?page_id=2 [L]
RewriteRule ^blog/about$ /blog/?page_id=2 [L]
Categories are similar:
RewriteRule ^blog/category/uncategorized/$ /blog/?cat=1 [L]
RewriteRule ^blog/category/zeitgeist/$ /blog/?cat=9 [L]
With Categories you need to set up the rules to match the category name with its id number. Although as of the latest iterations of WordPress, categories have been replaced with tags, and I haven’t fully crossed-over to that yet, so I’m not sure what’s involved there.