Ubuntu, Apache and making mod_rewrite happy

The fol­low­ing only applies to the Apache which ships with Ubuntu — so if you’re won­der­ing why your mod_rewrite isn’t work­ing with some­thing else, chances are this won’t be that helpful.

I was play­ing with mod_rewrite in the con­text of Word­Press 1.5 for use as a CMS on a new web­site, and it basi­cally wasn’t work­ing at all. The mod­ule was there, it was enabled, but it was doing absolutely noth­ing. I rein­stalled Apache man­u­ally (from source), and tried to do the painful stuff that goes with that (PHP ./configure options, any­one? Some peo­ple might be able to do it, but not me!), but gave up in dis­dain when I realised I had 2 hours to get work done before a meet­ing and didn’t have a work­ing Apache install to get it work­ing on. mod_rewrite or not, it was back to the stock install that “sort of” worked.

mod_rewrite wasn’t work­ing for the meet­ing, but that didn’t really mat­ter… I’ve dis­cov­ered that I care more about friendly URLs than most other peo­ple I’ve ever met in real life! That evening, I looked at the prob­lem again, and, after trawl­ing through apache2.conf (Ubuntu’s equiv­a­lent of httpd.conf — don’t ask why, I don’t think even they know) and talk­ing to Michael about it, I dis­cov­ered that I’d been look­ing in the wrong place.

It doesn’t mat­ter 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 vir­tual host setup under “sites-enabled” (actu­ally just a sym­link to “sites_available” — this is best prac­tice when cre­at­ing new vhosts, by the way: stick the con­fig in sites-available and sym­link to that from sites-enabled… there are a few rea­sons for this, which wouldn’t really affect the casual user, but if you were run­ning LOTS of web­sites off it it’d soon become help­ful if you needed to dis­able web­sites because of pol­icy vio­la­tions or billing with­out actu­ally delet­ing the con­fig­u­ra­tion.), instead of just the sin­gle “real” server. Okay, this prob­a­bly makes sense… but for the fact that it didn’t SAY it any­where, and addi­tion­ally, despite hav­ing sup­port for .htac­cess files built into the apache2.conf file, it’s set to do absolutely noth­ing in the vir­tual host con­fig “default”.

Basi­cally, to get mod_rewrite to work, load the mod­ule by sym­link­ing to mods-available/rewrite.conf in mods-enabled, then set AllowOver­ride to “all” in sites-available/default in both the root (“/”) con­tainer and the /var/www/ con­tainer. Whilst you’re there, it may be a good idea to get rid of the Redi­rect­Match, sim­ply because it’s annoy­ing… I haven’t, but only because I haven’t both­ered to put some­thing in the root of the default site.

This stuff is prob­a­bly self explana­tory to many, but it took me too long to realise it, even with help, and in my trawl­ings of Google I hadn’t found any­thing to say how to do it, so there we are.

Updated: see com­ments 3 and 4

# by Josh on March 6th, 2005 Tags: , , ,
| 63 Comments »