cat /proc/claus

because blogs never go out of style!

Redirecting URIs with ModRewrite

2024 November 30

Another night working on the blog. This time I wanted to do something about my ?p=XXX links.

At some point, wordpress started giving URIs to my posts that ended in ?p=XXX. The interrogation point indicates that the URI is a query -- in other word, a dynamic webpage would deal with the parameters after the P to choose what page to display.

The idea of a static blog is to not have a query. When I first set up eleventy, I thought of just biting the bullet and letting the link break. But as I explained in the last post, I changed my mind. So how could I transform the query links into normal links?

After some internet searching, turns out that the solution is to use ModRewrite which is an extension to Apache that allows for procedural replacements to parts of an URI at the HTTP server level. Now to be honest, I still don't quite completely understand how to make ModRewrite work, but diving into quite a few blogs and StackOverflow pages gave me some black magic spells that you can intonate to make things work... kinda.

So the idea is that you add rewrite rules to the .htaccess file in your blog. Mine looks like this now:

ErrorDocument 404 /404.html

RewriteEngine On

# RewriteRule index.html about.html

RewriteCond %{QUERY_STRING} p=([0-9]+)
RewriteRule ^(.*)$ archives/%1 [L]

The first line indicates the File Not Found page and is not really related to rewriting.

The second line tells ModRewrite to do its job. When I started figuring this out, I lost a lot of time until I learned that this was necessary.

The third line (commented) is a very simple example of a rewrite rule. Here it replaces index.html in the URI with about.html, so when you try to access the main page, you are redirected to the about page instead.

The fourth and fifth lines are the ones that do the heavy lifting. The fourth line defines that it is a rewrite rule that only happens when a query string described by the regular expression at the end of the line shows up. The fifth line says what needs to be rewritten: in this case, the entire uri (the relative part of it, at least) is replaced with archives/post-number.

It looks simple, but took me much of a night to make sure it worked like I wanted to. This was hard to learn because it fails silently -- there are no error messages when you mess something up, and it is really hard to get information about what is going wrong. I'm pretty sure that the solution above has some unknown errors and things waiting to go boom, but at least it works for now...

Well, not really...

After uploading some old posts, I noted that there were actually several patterns for wordpress internal links. The above modrewrite rules solves one of these patterns, but some are still left unsolved.

I just need to write more rules for the other patterns... but that is a task for another day!

Tagged: #blog, #eleventy, #hacking, #URI,