Josh (the blog)

Hey there. I’m Josh, a SydneyCanberra-based maker of Internets. I don’t update this very often.


@joahua

Ubuntu, Apache and making mod_rewrite happy

The following only applies to the Apache which ships with Ubuntu — so if you’re wondering why your mod_rewrite isn’t working with something else, chances are this won’t be that helpful.

I was playing with mod_rewrite in the context of WordPress 1.5 for use as a CMS on a new website, and it basically wasn’t working at all. The module was there, it was enabled, but it was doing absolutely nothing. I reinstalled Apache manually (from source), and tried to do the painful stuff that goes with that (PHP ./configure options, anyone? Some people might be able to do it, but not me!), but gave up in disdain when I realised I had 2 hours to get work done before a meeting and didn’t have a working Apache install to get it working on. mod_rewrite or not, it was back to the stock install that “sort of” worked.

mod_rewrite wasn’t working for the meeting, but that didn’t really matter… I’ve discovered that I care more about friendly URLs than most other people I’ve ever met in real life! That evening, I looked at the problem again, and, after trawling through apache2.conf (Ubuntu’s equivalent of httpd.conf — don’t ask why, I don’t think even they know) and talking to Michael about it, I discovered that I’d been looking in the wrong place.

It doesn’t matter whether rewrite.conf is in the mods-enabled folder (although it needs to be — but that’s not the issue at hand here) — by default, Ubuntu’s Apache install has a default virtual host setup under “sites-enabled” (actually just a symlink to “sites_available” — this is best practice when creating new vhosts, by the way: stick the config in sites-available and symlink to that from sites-enabled… there are a few reasons for this, which wouldn’t really affect the casual user, but if you were running LOTS of websites off it it’d soon become helpful if you needed to disable websites because of policy violations or billing without actually deleting the configuration.), instead of just the single “real” server. Okay, this probably makes sense… but for the fact that it didn’t SAY it anywhere, and additionally, despite having support for .htaccess files built into the apache2.conf file, it’s set to do absolutely nothing in the virtual host config “default”.

Basically, to get mod_rewrite to work, load the module by symlinking to mods-available/rewrite.conf in mods-enabled, then set AllowOverride to “all” in sites-available/default in both the root (“/”) container and the /var/www/ container. Whilst you’re there, it may be a good idea to get rid of the RedirectMatch, simply because it’s annoying… I haven’t, but only because I haven’t bothered to put something in the root of the default site.

This stuff is probably self explanatory to many, but it took me too long to realise it, even with help, and in my trawlings of Google I hadn’t found anything to say how to do it, so there we are.

Updated: see comments 3 and 4