Usually .htaccess for WordPress is set automatically when you change the permalink settings.

Unfortunately for me the WordPress system was not working properly. When I wrote the first posts on this blog however I did not bother to deal with the issue. Doing so leaves you with two problems:

  1. How to set .htaccess
  2. How to ensure the links to your old posts get redirected

After reading the great mod_rewrite documentation and combining that with a great .htaccess files here my solution.

In order to deal with the old posts:

RewriteCond %{QUERY_STRING} ^p=11$
RewriteRule ^$ http://www.mellowmorning.com/2007/08/27/picture-resizing-on-steroids/? [R=301,L]
RewriteCond %{QUERY_STRING} ^p=3$
RewriteRule ^$ http://www.mellowmorning.com/2007/08/18/ten-reasons-why-symfony-rocks-part-1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^p=4$
RewriteRule ^$ http://www.mellowmorning.com/2007/08/12/barcamp-events/? [R=301,L]
RewriteCond %{QUERY_STRING} ^feed=atom&cat=1$
RewriteRule ^$ http://www.mellowmorning.com/category/symfony/feed/? [R=301,L]

Note that HTTP_HOST and QUERY_STRING are by not included in the conditions checked by RewriteRule. The rewrite rule only gets the part after the / and before the ?. That is why the combination of RewriteCond and RewriteRule is used.

To add the www. to the link:

RewriteCond %{HTTP_HOST} ^mellowmorning.com$ [NC]
RewriteRule ^(.*)$ http://www.mellowmorning.com/$1 [R=301,L]

Alternatively to remove it:

RewriteCond %{HTTP_HOST} ^www\.([^\.]+\.[^\.]+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Dealing with trailing slashes:

RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1/ [R=301,L]

And here finally the total result:

It was great fun to learn more about this excellent feature. Be sure to check the mod_rewrite documentation, it is written very well.

<IfModule mod_rewrite.c>
RewriteEngine On
# Deal with three old posts
RewriteCond %{QUERY_STRING} ^p=11$
RewriteRule ^$ http://www.mellowmorning.com/2007/08/27/picture-resizing-on-steroids/? [R=301,L]
RewriteCond %{QUERY_STRING} ^p=3$
RewriteRule ^$ http://www.mellowmorning.com/2007/08/18/ten-reasons-why-symfony-rocks-part-1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^p=4$
RewriteRule ^$ http://www.mellowmorning.com/2007/08/12/barcamp-events/? [R=301,L]
RewriteCond %{QUERY_STRING} ^feed=atom&cat=1$
RewriteRule ^$ http://www.mellowmorning.com/category/symfony/feed/? [R=301,L]
# If subdomain www exists, remove it first
#RewriteCond %{HTTP_HOST} ^www\.([^\.]+\.[^\.]+)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# If subdomain does not exists, add it first
RewriteCond %{HTTP_HOST} ^mellowmorning.com$ [NC]
RewriteRule ^(.*)$ http://www.mellowmorning.com/$1 [R=301,L]
# If requested resource does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# and does not end with a period followed by a filetype
RewriteCond %{REQUEST_URI} !..+$
# and does not end with a slash
RewriteCond %{REQUEST_URI} !/$
# then add a trailing slash and redirect
RewriteRule (.*) $1/ [R=301,L]
</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