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

Tags: , , ,

posted on Sunday, March 6th, 2005 at 3:08 pm by Josh, filed under Geek, Open Source.

70 Responses to “Ubuntu, Apache and making mod_rewrite happy”

  1. dale says:

    Good infor­ma­tion *will remem­ber if I try ubuntu*.

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

  2. Brian Heung says:

    Thanks for your hint, I found this on Google and it helped my gallery instal­la­tion on Ubuntu a lot.

  3. Karin says:

    I have one lit­tle remark, it’s not mods_enabled and mods_available but mods-enabled and mods-available (dash instead of under­score), sub­tle dif­fer­ence, but it makes all the dif­fer­ence when try­ing to locate these dirs if you don’t know where they are.

    Other than that, thanks, it worked perfectly!

  4. Josh says:

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

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

  6. Sencer says:

    sudo a2enmod rewrite

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

  7. Wayne says:

    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

  8. prash says:

    can­not thank you enough! i had the same prob­lem with word­press and your one trick of “set AllowOver­ride to “all”” did it!
    thanks again!
    –prash.

  9. gumboyaya says:

    Thanks, this help me fix gallery. Regards –GyY

  10. Andrew Bruce says:

    Thanks for this. Made my point­less change from Gen­too to Ubuntu eas­ier. Why on Earth isn’t this enabled by default? Guess it’s a Debian secu­rity para­noia issue.

  11. Justin says:

    Another per­son you helped with Gallery ^^;

  12. Xicheng says:

    This post is very help­ful but might be much for con­fig­ur­ing Apache mod_rewrite with word­press which need per-directory configuration(.htaccess).

    If you are like me and con­fig­ure mod_rewrite from Apache Server side(i.e. the Ubuntu default Apache2 Server) instead of .htac­cess, you might not need to change AllowOver­ride direc­tive. the fol­low­ing 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 con­tainer of your sites-configuration file instead of in the file “/etc/apache2/mods-enabled/rewrite.conf”.

    hope this help..
    Xicheng

  13. CleverShark says:

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

    Great arti­cle, it’s very useful!

  14. Meegaha says:

    Ah! Thanks for this. Was stuck on the first step of try­ing out the Zend frame­work work­ing; till google gave me this link. You just saved me hours of wasted time. Cheers…

  15. calethix says:

    I’ve been think­ing about switch­ing from Man­driva to kubuntu for a while and ran into prob­lems with mod_rewrite not work­ing. I spent prob­a­bly 2-3hrs last night try­ing stuff and read­ing var­i­ous things I found on google. At first I thought my .htac­cess was messed up until I made the rewrite com­mands as sim­ple as pos­si­ble and it still didn’t work.
    Did another search today and found this page which pointed me right to the prob­lem in the sites-available/default folder. Thanks :)

  16. Trevor says:

    Thanks, this was exactly what I was look­ing for. Now my word­press perma­links work!

  17. Seth says:

    Another thank you. Scratch­ing my head why a local apache2 install on my lap­top wasn’t work­ing with mod_rewrite. Google found your easy answer.

  18. Greg says:

    Another per­son you helped — this one with dru­pal! Thanks loads for this clear, con­cise and use­ful post!

  19. Brett says:

    This tip helped me as well play­ing with dru­pal. Thanks for the post :-)

  20. Andres says:

    Wow. Found this on Google and it was exactly what I was look­ing for. I thought I had to recom­pile Apache 2.0 (which I have zero idea how to do; I can­not live with­out apt-get!), but after read­ing this I looked in my modes-available direc­tory and there it was!! Duh! I should have looked there. Thanks!

  21. Dennis says:

    Great Arti­cle!

    This makes mod_rewrite and ME happy!

    Thx a bunch

  22. Derrik says:

    Thanks man, that worked great! I’m installing CakePHP on Ubuntu and the test page wasn’t load­ing the CSS or images prop­erly. But after I set AllowOver­ride to ‘all’ in the / and /var/www/ direc­to­ries in the /etc/apache2/sites-available/default file and cre­ated a sym­link (or ln –sf ) it worked great. The only dif­fer­ence is that my sym­link file was rewrite.load, not conf. Not sure why, but it works now, thanks!

  23. Loc says:

    I do every­thing I found on Google to enable mod_rewrite, (includ­ing sudo a2enmod rewrite ), but it just didn’t work. Then I found this. A mil­lion thanks my friend!

  24. Mark says:

    Another soul saved here. Cheers.

  25. John S says:

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

  26. Herbert says:

    Thanks, very helpfull !

  27. Zak says:

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

    excel­lent tutorial

  28. Mike D says:

    I still can’t get it to work. I have tried every com­bi­na­tion of the var­i­ous changes described in this arti­cle and its com­ments 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 RerwriteL­ogLevel state­ments for debug­ging. The log file I spec­i­fied was cre­ated the next time I restarted apache, but noth­ing ever gets writ­ten to it (it is 0 bytes in size). Any­body have any thoughts?

  29. Mike D says:

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

  30. Mike D says:

    Addi­tional update: in place of the RewriteRule state­ments in /etc/apache2/sites-available/default, I put an Include state­ment for httpd.conf, and that worked. I removed the Include of httpd.conf from apache2.conf. I think it has some­thing to do with the RewriteRule state­ments being called from within the con­text of the VirtualHost.

  31. mk says:

    How if the doc­u­ment is out of /var/www/ ? Cause I am too lazy if script­ing via ter­mi­nal and sudo. Many thanks to you. I’m sorry aboute my english.

  32. Bill says:

    Yes, thanks so much. I have been read­ing a book Search Eng­ing Opti­miza­tions with PHP and it has good point­ers and tests for mod_rewrite, but it didn’t men­tion set­ting the AllowOver­ride to all. That did the trick! Thanks for post­ing this.

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

  34. Daniel says:

    Joshua, you’re a genius. :) I’d been fight­ing it for a long time, and had given up. I guess I used dif­fer­ent search terms, because your site was #3 for the ones I used this time, and your change (AllowOver­ride all instead of None) is what made it work.

    Thanks!

  35. Chris says:

    Absolutely bril­liantly right on the dot for what I needed on my desk­top devel­op­ment box I use for work!
    You sir, are the man.

  36. Merrioc says:

    you could also just put the con­tents of your .htac­cess file in the site conf file in a

    and change the over­ride at that level rather then the server root.

  37. raphaele says:

    thanks… iwould have spent hours on that with­out u ;-) !!!

  38. manuel says:

    THANKS A LOT!, after 3 hours research­ing this prob­lem in m y ubuntu box, i found you… thanks dude!

  39. pambuk says:

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

  40. Vinay Kumar Singh says:

    Thanks a lot…

  41. Leviticuz says:

    Thanx for the tip, I’ve been search­ing of why mod-rewrite would spit out 404 errors on my Joomla instal­la­tion, seems “sudo a2enmod rewrite” is not the only­thing I need to do.

    I did fig­ure it had to do with “AllowOver­rides” though. Thanx a lot.

  42. Adan Nada says:

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

  43. Swift Arrow says:

    It had me won­der­ing for a while… I was lucky to come across this site pretty early on!
    Keep up the good work!

  44. Halibutt says:

    Thanks mate, this helped (at last).

  45. diyism says:

    Guys, why not adopt XiCheng’s method at floor 12,
    need not fuck­ing AllowOverrides.

    sudo ln –s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load

    vi /etc/apache2/sites-enabled/000-default

    NameVir­tu­al­Host *


    RewriteEngine on
    RewriteRule ^/(.+)\.(exe|zip|rar)$ /!index/download.php?file=/$1.$2

    sudo /etc/init.d/apache2 restart

  46. diyism says:

    vi /etc/apache2/sites-enabled/000-default

    NameVir­tu­al­Host *
    <Vir­tu­al­Host *>

    RewriteEngine on
    RewriteRule ^/(.+)\.(exe|zip|rar)$ /!index/download.php?file=/$1.$2
    </VirtualHost>

  47. najs says:

    unfor­tu­nately, it doesn’t work for me…and I really don’t know what am I doing wrong. All is set as it is described here, mod_rewrite works nice on my web­site, but not at local­host. Any ideas?

  48. najs says:

    found a mis­take, sorry … a stu­pid mis­take with the default con­fig file (I had 2!!!! with dif­fer­ent set­tings)…
    thanks a lot for tip :)

  49. Richard says:

    Thanks SO MUCH! This finally fixed a WordPress-MU prob­lem I was hav­ing. I didn’t know you had to Allow at the base level for Alias Allow’s to work as well. Happy Happy Joy Joy!

  50. mostar says:

    Hi
    I have the same prob­lem about mod_rewrite
    I want install Sil­ver­Stripe on Local­Host XAMPP
    I’m not a pro­gram­mer and i dont under­e­stand your descrip­tion
    Tell me eas­ily please

  51. jam says:

    Another grate­ful reader — I had prob­lems mak­ing clean urls with pmwiki, and couldn’t fig­ure out why my setup on my own Ubuntu box was any dif­fer­ent from the one on red hat at work.
    Thanks.

  52. nsanetrinity says:

    I was as giddy as a lit­tle school girl. I lit­er­ally stood up cheered and laugh­ing. Even shed a tear of joy after get­ting this to work. Any­one work­ing for Ubuntu here’s your sign. WAAAHOOOOOOOOOOOOOO!

  53. Roberto says:

    Thanks fot pub­lish­ing this tip, i was strug­gling mak­ing cakephp work. This solved my problem.

  54. Golovan says:

    Tnx for this!
    It also worked for me. Been try­ing to get an Elgg install up for some hours:)
    Had missed AllowOver­ride All in /var/www Directory

  55. izua says:

    thanks, this saved a bunch of hours.

  56. faniesmith says:

    Your the man! Thanks so much.

  57. […] pas « stan­dard » sur Ubuntu. J’ai donc suivi les recom­man­da­tions du blog de Josh Street pour cor­riger le […]

  58. […] Basi­cally, to get mod_rewrite to work, load the mod­ule by 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 (Copy & paste from here). […]

  59. Semo says:

    thanks man, i’ve seen a lot of solu­tion but this is the real one. thanks again.

  60. Zair says:

    THanks man, you are life saver, “AllOver­ride all” did the job after enabling mod_rewrite

  61. Igor says:

    Thank you, very match )

  62. Milan says:

    Hi, Thanks a lot. really use­ful. This solved my problem.

  63. alessio says:

    Oh my god this should be in the man­ual of LAMP for Ubuntu :)

  64. Sorin says:

    Works like a charm! Thanks

  65. RET says:

    Six and a half years on, this page is still a very handy resource. Thanks.

  66. I can’t make this s*** work!

    I know is because “Basi­cally, to get mod_rewrite to work, load the mod­ule by sym­link­ing to mods-available/rewrite.conf in mods-enabled”.
    But i can’t find the file ‘rewrite.conf’ so i can’t make the bloody link…

    I will never under­stand why the author didn’t pro­vide some ter­mi­nal com­mand… i guess he likes poetry and talks.

    Use­less arti­cle for me and a big waste of time. I’m started to get sick­ened by Ubuntu: in the last 2 days 0 work @ my projects but full time work in mak­ing mysql, php­myad­min, word­press, net­beans and every­thing work on Ubuntu. I hope this pays off… i really really do!

  67. I was so frus­trated when i wrote the above. It worked after i fig­ured this: http://ubuntuforums.org/showthread.php?p=11326472

    So this arti­cle is still good even after 6 years… amazing!

  68. Why is this the default with­out say­ing it any­where?! Thanks so much, I’ve been strug­gling for days try­ing to get mod_rewrite to work with my Elgg install but no luck. After read­ing your arti­cle every­thing is solved in a jiffie, I can’t thank you enough!

Leave a Reply