A few days ago, I vented about the blog's URLs, which ended up causing Google to see a flood of 404s — and, more importantly, several of my own bookmarks broke. Annoying.

So I modified the .htaccess file, using the apache server feature to set up redirects.

Constraint: apache server.

Current environment: Apache/2.2.15

Goals:

  1. Update post URLs, i.e. change everything of the form "http://argcv.com/archives/1234" to "http://argcv.com/articles/1234.c"

  2. Update tag and category URLs, i.e.

The approach is straightforward:

Edit the .htaccess file in the root directory and add the following:

# Redirect Start

RewriteEngine On
RewriteRule ^archives/(\d*)$ /articles/$1.c [R=301,L]
RewriteRule ^archives/(.*)$ /articles/$1 [R=301,L]

# Redirect End

Where,

  • "^" matches the beginning
  • ".*" matches any characters of any length
  • "\d*" matches digits of any length
  • "$1" refers to the Nth captured group, i.e., the content inside ()

PS:

This was originally supposed to be appended at the end of the .htaccess file, but it simply didn't work! Annoyed, I moved it to the top of the file, and it worked right away. Some other content in the file must have gotten in the way.

References: