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:
Update post URLs, i.e. change everything of the form "http://argcv.com/archives/1234" to "http://argcv.com/articles/1234.c"
Update tag and category URLs, i.e.
- Change all URLs of the form "http://argcv.com/archives/tag/xxx" to "http://argcv.com/articles/tag/xxx"
- Change all URLs of the form "http://argcv.com/archives/category/xxx" to "http://argcv.com/articles/tag/xxx"
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 EndWhere,
- "^" 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:
- A very handy htaccess testing tool: http://htaccess.madewithlove.be
- htaccess 301 redirect with regular expressions http://superuser.com/questions/155139/htaccess-301-redirect-with-regular-expressions

Comments · 3