Josh.st

Websites, English, Education, and miscellaneous nerdery

Sunday, 6th March, 2005

Ubuntu, Apache and making mod_rewrite happy

Filed under: Geek, Open Source — Josh @ 3:08 pm

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



43 Comments »

  1. Good information *will remember if I try ubuntu*.

    I had the same problem (sort of) on my mac. It was a pain because of the crazy root access, or lack of.

    Comment by dale — Sunday, 6th March, 2005 @ 5:50 pm

  2. Thanks for your hint, I found this on Google and it helped my gallery installation on Ubuntu a lot.

    Comment by Brian Heung — Sunday, 13th March, 2005 @ 10:11 pm

  3. I have one little remark, it’s not mods_enabled and mods_available but mods-enabled and mods-available (dash instead of underscore), subtle difference, but it makes all the difference when trying to locate these dirs if you don’t know where they are.

    Other than that, thanks, it worked perfectly!

    Comment by Karin — Thursday, 7th April, 2005 @ 10:04 pm

  4. Oh, of course! Sorry about that, post edited.

    Comment by Josh — Friday, 8th April, 2005 @ 7:46 am

  5. [...] buntu server, but I finally got it right. Here’s a quick and easy guide for doing it here. Before following the guide, make sure you have apache-util [...]

    Pingback by Whatever I Want » Blog Archive » Got permalinks working — Thursday, 5th May, 2005 @ 2:19 pm

  6. sudo a2enmod rewrite

    will do it for you. ;) But thanks for the pointer.

    Comment by Sencer — Friday, 6th May, 2005 @ 4:22 am

  7. Thanks alot for the tip, worked great.
    This may be a Debian thing.
    I used this tip on a Mepis install and both Ubuntu and Mepis
    are based on Debian

    Comment by Wayne — Monday, 9th May, 2005 @ 3:26 am

  8. cannot thank you enough! i had the same problem with wordpress and your one trick of “set AllowOverride to “all”” did it!
    thanks again!
    -prash.

    Comment by prash — Saturday, 9th July, 2005 @ 2:02 am

  9. Thanks, this help me fix gallery. Regards -GyY

    Comment by gumboyaya — Thursday, 25th August, 2005 @ 7:24 am

  10. Thanks for this. Made my pointless change from Gentoo to Ubuntu easier. Why on Earth isn’t this enabled by default? Guess it’s a Debian security paranoia issue.

    Comment by Andrew Bruce — Sunday, 4th December, 2005 @ 6:44 am

  11. Another person you helped with Gallery ^^;

    Comment by Justin — Sunday, 12th February, 2006 @ 6:41 am

  12. This post is very helpful but might be much for configuring Apache mod_rewrite with wordpress which need per-directory configuration(.htaccess).

    If you are like me and configure mod_rewrite from Apache Server side(i.e. the Ubuntu default Apache2 Server) instead of .htaccess, you might not need to change AllowOverride directive. the following steps worked for me.
    1) % sudo a2enmod rewrite
    2) % sudo /etc/init.d/apache2 force-reload
    3) % sudo vi /etc/apache2/sites-enabled/000-default

    ………………

    RewriteEngine on
    RewriteRule **************

    ………………

    4) % apache2ctl graceful

    You may need to put your RewriteRule within container of your sites-configuration file instead of in the file “/etc/apache2/mods-enabled/rewrite.conf”.

    hope this help..
    Xicheng

    Comment by Xicheng — Monday, 13th February, 2006 @ 11:02 am

  13. Just as a note — in 6.0.6 the entry for mod_rewrite in the mods-available directory has been changed to rewrite.load.

    Great article, it’s very useful!

    Comment by CleverShark — Thursday, 17th August, 2006 @ 11:28 pm

  14. Ah! Thanks for this. Was stuck on the first step of trying out the Zend framework working; till google gave me this link. You just saved me hours of wasted time. Cheers…

    Comment by Meegaha — Sunday, 15th October, 2006 @ 8:30 am

  15. I’ve been thinking about switching from Mandriva to kubuntu for a while and ran into problems with mod_rewrite not working. I spent probably 2-3hrs last night trying stuff and reading various things I found on google. At first I thought my .htaccess was messed up until I made the rewrite commands as simple as possible and it still didn’t work.
    Did another search today and found this page which pointed me right to the problem in the sites-available/default folder. Thanks :)

    Comment by calethix — Saturday, 28th October, 2006 @ 4:04 am

  16. Thanks, this was exactly what I was looking for. Now my wordpress permalinks work!

    Comment by Trevor — Friday, 17th November, 2006 @ 6:09 am

  17. Another thank you. Scratching my head why a local apache2 install on my laptop wasn’t working with mod_rewrite. Google found your easy answer.

    Comment by Seth — Monday, 25th December, 2006 @ 2:21 pm

  18. Another person you helped - this one with drupal! Thanks loads for this clear, concise and useful post!

    Comment by Greg — Wednesday, 7th February, 2007 @ 2:41 am

  19. This tip helped me as well playing with drupal. Thanks for the post :-)

    Comment by Brett — Monday, 19th February, 2007 @ 10:50 am

  20. Wow. Found this on Google and it was exactly what I was looking for. I thought I had to recompile Apache 2.0 (which I have zero idea how to do; I cannot live without apt-get!), but after reading this I looked in my modes-available directory and there it was!! Duh! I should have looked there. Thanks!

    Comment by Andres — Saturday, 24th February, 2007 @ 2:41 pm

  21. Great Article!

    This makes mod_rewrite and ME happy!

    Thx a bunch

    Comment by Dennis — Monday, 5th March, 2007 @ 11:32 pm

  22. Thanks man, that worked great! I’m installing CakePHP on Ubuntu and the test page wasn’t loading the CSS or images properly. But after I set AllowOverride to ‘all’ in the / and /var/www/ directories in the /etc/apache2/sites-available/default file and created a symlink (or ln -sf ) it worked great. The only difference is that my symlink file was rewrite.load, not conf. Not sure why, but it works now, thanks!

    Comment by Derrik — Friday, 6th April, 2007 @ 8:50 pm

  23. I do everything I found on Google to enable mod_rewrite, (including sudo a2enmod rewrite ), but it just didn’t work. Then I found this. A million thanks my friend!

    Comment by Loc — Sunday, 29th April, 2007 @ 10:31 am

  24. Another soul saved here. Cheers.

    Comment by Mark — Thursday, 10th May, 2007 @ 1:43 am

  25. Thanks!

    Comment by Mike McG — Thursday, 10th May, 2007 @ 1:58 pm

  26. Thanks a lot! You have no idea how long I’ve been trying to sort mod_rewrite out! Took me ages to find this article.

    Comment by John S — Wednesday, 30th May, 2007 @ 5:15 am

  27. Thanks, very helpfull !

    Comment by Herbert — Tuesday, 12th June, 2007 @ 4:54 am

  28. just open /etc/apache2/sites-available/000-default and set AllowOverride to All
    then restart apache using /etec/init.d/apache2 force-reload

    excellent tutorial

    Comment by Zak — Tuesday, 3rd July, 2007 @ 7:50 am

  29. I still can’t get it to work. I have tried every combination of the various changes described in this article and its comments that I could think of. Using Ubuntu 6.06.1. All my RewriteRules are defined in httpd.conf, which is included via apache2.conf. I set RewriteEngine On, and also added RewriteLog and RerwriteLogLevel statements for debugging. The log file I specified was created the next time I restarted apache, but nothing ever gets written to it (it is 0 bytes in size). Anybody have any thoughts?

    Comment by Mike D — Thursday, 5th July, 2007 @ 8:04 am

  30. Update: I got it to work by putting the RewriteRule statements in /etc/apache2/sites-available/default. Not sure why they didn’t work from httpd.conf.

    Comment by Mike D — Thursday, 5th July, 2007 @ 8:29 am

  31. Additional update: in place of the RewriteRule statements in /etc/apache2/sites-available/default, I put an Include statement for httpd.conf, and that worked. I removed the Include of httpd.conf from apache2.conf. I think it has something to do with the RewriteRule statements being called from within the context of the VirtualHost.

    Comment by Mike D — Thursday, 5th July, 2007 @ 3:56 pm

  32. How if the document is out of /var/www/ ? Cause I am too lazy if scripting via terminal and sudo. Many thanks to you. I’m sorry aboute my english.

    Comment by mk — Friday, 6th July, 2007 @ 1:20 am

  33. Yes, thanks so much. I have been reading a book Search Enging Optimizations with PHP and it has good pointers and tests for mod_rewrite, but it didn’t mention setting the AllowOverride to all. That did the trick! Thanks for posting this.

    Comment by Bill — Friday, 13th July, 2007 @ 6:43 am

  34. THanks so much for this! You don’t know how long I’ve been trying to fix it. I guess I just assumed rewrites would be allowed by default vhost!

    Comment by Luke Hoersten — Monday, 16th July, 2007 @ 1:57 pm

  35. Joshua, you’re a genius. :) I’d been fighting it for a long time, and had given up. I guess I used different search terms, because your site was #3 for the ones I used this time, and your change (AllowOverride all instead of None) is what made it work.

    Thanks!

    Comment by Daniel — Wednesday, 31st October, 2007 @ 11:45 am

  36. Absolutely brilliantly right on the dot for what I needed on my desktop development box I use for work!
    You sir, are the man.

    Comment by Chris — Wednesday, 5th December, 2007 @ 3:53 am

  37. you could also just put the contents of your .htaccess file in the site conf file in a

    and change the override at that level rather then the server root.

    Comment by Merrioc — Sunday, 30th December, 2007 @ 6:34 pm

  38. thanks… iwould have spent hours on that without u ;-) !!!

    Comment by raphaele — Friday, 11th January, 2008 @ 8:01 pm

  39. THANKS A LOT!, after 3 hours researching this problem in m y ubuntu box, i found you… thanks dude!

    Comment by manuel — Thursday, 24th January, 2008 @ 10:00 am

  40. Thanks man, I just bought a new machine and was enjoying it, ’till I got stuck with mod_rewrite. Works like a charm now, take care :)

    Comment by pambuk — Thursday, 7th February, 2008 @ 6:19 am

  41. Thanks a lot…

    Comment by Vinay Kumar Singh — Wednesday, 2nd April, 2008 @ 7:25 pm

  42. Thanx for the tip, I’ve been searching of why mod-rewrite would spit out 404 errors on my Joomla installation, seems “sudo a2enmod rewrite” is not the onlything I need to do.

    I did figure it had to do with “AllowOverrides” though. Thanx a lot.

    Comment by Leviticuz — Sunday, 1st June, 2008 @ 7:38 pm

  43. Good one man!
    This helped me with my CakePHP installation.

    Comment by Adan Nada — Monday, 28th July, 2008 @ 9:34 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

If your comment does not immediately appear it has probably been caught in a moderation queue, and will be manually approved in the near future. There is no need to resubmit your comment.

Name and email address are both optional, but recommended. Telephone usage is a completely unrelated act, and should bear no influence on your willingness to participate in the discussion. Or criticism. Or whatever. Hurry up and post already.

Powered by WordPress. Copyright © 2004–2007, Joshua Street.