Posts Tagged ‘htaccess’

So, I noticed WordPress 2.3 was out, and upgraded with no issues. Of course, then I switched all my categories into tags, and changed my permalinks to leave out my nickname (something I couldn’t do with the old WordPress I was running) and things started going missing.

Using some .htaccess rules I was able to redirect my category index pages to the new tag pages fairly successfully, so that people’s bookmarks and feedreaders (don’t forget people can subscribe to feeds for all of these weird index pages) in what I hope is a clean way:


# Keep this out of the wordpress section, or it will get overwritten
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# rewrite categories as tags
RewriteRule ^category/(.+)$ tag/$1 [redirect=permanent,last]
# rewrite posts by my nick to my new /%postname%/ permalinks
RewriteRule ^jaykul/(.+)$ $1 [redirect=permanent,last]
# rewrite up to four levels of nested categories as tag intersections
# But don't forget to allow access to the feeds
RewriteRule ^tag/([^/+,]+)/(?!feed|rss|rss2|atom)([^/+,]+)/?$ tag/$1+$2/              [redirect=permanent,last]
RewriteRule ^tag/([^/+,]+)/([^/+]+)/(feed|rss|rss2|atom)/?$ tag/$1+$2/$3              [redirect=permanent,last]
RewriteRule ^tag/([^/+,]+)/([^/+]+)/([^/+]+)/?$ tag/$1+$2+$3/                         [redirect=permanent,last]
RewriteRule ^tag/([^/+,]+)/([^/+]+)/([^/+]+)/(feed|rss|rss2|atom)/?$ tag/$1+$2+$3/$4  [redirect=permanent,last]
RewriteRule ^tag/([^/+,]+)/([^/+]+)/([^/+]+)/([^/]*)/?$ tag/$1+$2+$3+$4/              [redirect=permanent,last]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
 

Some interesting things to note:

  • You have to redirect your categories to the tags page so that user agents (browsers and RSS readers) can update their links (if they bother, sigh).
  • You have to take special care of the fact that people can reach the rss feeds as categoryname/feed/ or categoryname/feed/rss/ or categoryname/rss/ ...
  • You need to be careful with the redirects because you can cause infinite loops.
  • You can link to tags and their feeds as UNIONS or as INTERSECTIONS, but not both.

Did you know that people can navigate to pages for multiple tags? In fact, /tags/powershell+development/ will link to items tagged with both powershell and development, whereas /tags/powershell,scripting/atom is the Atom feed for items tagged with either PowerShell or Scripting (or both). I wrote my rules to target the intersection, since that’s sort-of how categories worked so it most nearly preserves my previously working feeds.

I also picked a random new theme, as you can see — I’ve been working on it for a couple of evenings, and there’s still more work to do. So far I’ve widget-enabled it, and partially tag-enabled it … I’m going through the process of modifying a couple of my plugins into widgets (how did I miss this so completely in the past?), and then I’ll hide that extra sidebar on the post pages, and make it variable width.

I suppose I need to modify the header too, it’s hard to read, and if anyone has any ideas about a better way to present code … I’m open to suggestions. Of course… I also need to make it all validate XHTML 1.1 Strict (‘cause I’m like that).

Search My Content