Enable mod_rewrite on OS X 10.4 (Tiger)

Tiger has introduced a new super-confusion level to the stock configuration of Apache. In addition to the httpd.conf file in the /etc/httpd directory, there’s now a new users directory as well. That directory holds unique config files for each user of the machine. So, if you were to enable mod_rewrite or AllowOverrides in httpd.conf, you may find that it doesn’t quite cut the mustard in your personal Sites directory. Let’s take a look:

To enable mod_rewrite:

  1. Open /etc/httpd/httpd.conf
    (I highly recommend TextMate - from the command line you can simply type this:
    $ mate /etc/httpd/httpd.conf
    or use the old standards: vi, vim, whathaveyou)
  2. Go to line 223 (if your config file just so happens to jive with mine) and uncomment the following line:
    LoadModule rewrite_module libexec/httpd/mod_rewrite.so
    (mind the wrap)
  3. Go to line 267 and uncomment the following line:
    AddModule mod_rewrite.c
  4. Scroll down to line 408 and change the line to read:
    AllowOverride All
    (Some server admins will tell you this may not be the best idea for hosting a live site, but I’m assuming you’re using this for local development only, right?)
  5. Uncomment line 454:
    AccessFileName .htaccess
  6. Restart Apache:
    $ sudo apachectl restart

At this point you should have mod_rewrite happily fixing your ugly URL’s in the /Library/WebServer/Documents directory, but it’s not working in your /Users/you/Sites directory. What gives? Here’s the trick:

  1. Open the yourname.conf file in the /etc/httpd/users folder.
  2. Change the first two lines to this:
    Options All
    AllowOverride All
  3. Give Apache another bounce:
    $ sudo apachectl restart

You should now be seeing friendly url’s in your very own Sites directory.

29 Comments

  1. Jon says:

    I followed these instructions to the letter (thanks for posting them), and I’m now trying to get friendly URLs to work for this WordPress plug-in:

    http://www.randombyte.net/blog/2005/11/20/falbum-056/

    The .htaccess file in the relevant directory now contains:

    # BEGIN FAlbum

    RewriteEngine On
    RewriteRule ^photos/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?([^/]*)?/?$ /~jonzahlaway/zahlaway/wp-content/plugins/falbum/falbum-wp.php?$1=$2&$3=$4&$5=$6&$7=$8 [QSA,L]

    # END FAlbum

    Nonetheless, when I point my browser to /~jonzahlaway/zahlaway/photos/, I’m getting a 404, and when I point to /~jonzahlaway/zahlaway/wp-content/plugins/falbum/ it loads fine. Anyone know what the problem might be?

  2. robert says:

    Thank you SOOOO much! Over at drupal.org no one (including me) has ever heard of yourname.conf or so it seems.

  3. mozey says:

    Jon,

    I’m doing the same that you are doing (wordpress on mac), however, i put my wordpress in the main server page, so there is no ~/username, its in domain root. Oh, i also made one of the domains i dont ues point to my home machine. However, i’m sure somehow, it could work with localhost.

    Eitherways, i’m suck too, i’m getting a 404, No idea where to go!.

  4. midiman says:

    After lots of pain, I’ve found a solution that works for me on osx 10.3.9

    In userName.conf:

    DocumentRoot /Users/userName/Sites/
    ServerName 127.0.0.1/~userName/

    Options Indexes MultiViews
    AllowOverride All AuthConfig
    Order allow,deny
    Allow from all

    In a .htaccess file located in /Users/userName/Sites/:

    Options FollowSymlinks
    RewriteEngine on

    In all .htaccess files located in every site I want to mod_rewrite (all placed into main Sites folder, of course):

    Options FollowSymlinks
    RewriteEngine on

    Of course you have to write some rules in your .htaccess files for it to work 8)
    Regards from Spain

  5. midiman says:

    Oh, oh, all tags are gone, I think I should have put some code tag or something. I’ll try it one more time, but if it doesn’t work…
    In username.conf:
    [code]

    DocumentRoot /Users/jaimeort/Sites/
    ServerName 127.0.0.1/~jaimeort/

    Options Indexes MultiViews
    AllowOverride All AuthConfig
    Order allow,deny
    Allow from all

    [/code]

  6. midiman says:

    Oh man, tags are gone again 8( I’m really sorry, this is the las try, I’ll use html entities…
    At least it’ll be readable..

    <VirtualHost 127.0.0.1:80>
    DocumentRoot /Users/jaimeort/Sites/
    ServerName 127.0.0.1/~jaimeort/
    <Directory "/Users/jaimeort/Sites/">
    Options Indexes MultiViews
    AllowOverride All AuthConfig
    Order allow,deny
    Allow from all
    </Directory>
    </VirtualHost>

  7. Galen says:

    Excellent. Many thanks for this. Nice and simple and got it working perfectly first time. Thanks from New Zealand.

  8. Beck says:

    Worked like a charm! Thanks.

  9. Ken says:

    This was a huge help, thank you!

  10. romeroax says:

    You the man. This was the info I needed to know!

  11. Matt says:

    pretty sure i followed to the letter… still not working…. using osx 10.4.9 … anything a mac rookie might be missing here??

    used terminal to issue shell commands and text mate to edit… (many of the lines said to ‘un-comment’ were already uncommented… not sure if that’s any significance… )

    i just want mod-rewrite to work on my new mac. :)

  12. Michael says:

    Matt - if you don’t have your hosts file set to make paths root relative (I plan on writing a little tutorial on that one of these days) make sure you’re using a base href tag in the <head> tag of your document…

    See this link for a little more info:
    http://www.w3schools.com/tags/tag_base.asp

  13. Matt says:

    Thanks for the tip, got a question though… how would a base tag in the head of the destination document help me when htaccess is failing to load the document in the first place? I can type a test url in my browsers address bar:
    ——————————————————
    http://0.0.0.0/~username/sitefolder/test/
    ——————————————————

    Where ‘test’ is not a real folder in my tree

    and in my htacces:
    ——————————————————
    Options FollowSymlinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^/sitefolder/$
    RewriteRule ^.*$ http://0.0.0.0%{REQUEST_URI} [R=permanent,L]

    rewriteRule ^$ http://0.0.0.0/~username/sitefolder/realfile.php?r={REQUEST_URI}&%{QUERY_STRING} [L]
    rewriteRule ^/$ http://0.0.0.0/~username/sitefolder/realfile.php?r={REQUEST_URI}&%{QUERY_STRING} [L]

    rewriteRule ^test/$ http://0.0.0.0/~username/sitefolder/realfile.php?r={REQUEST_URI}&%{QUERY_STRING} [L]

    ———————————————————–
    I hope all those characters translate through the comment engine. ;)

  14. Dennis says:

    Thank you so much for this tutorial!! I was trying to get mod_rewrite to work on my local Tiger client Mac … never got it working using other tutorials.

    I’m testing it using Wordpress wich had an option to enable the use of search-engine-friendly-urls’ or ‘permalinks’. Worked online but not local.

    Thanks again. Now I can start learning mod_rewrite … anyone have any good tutorials for beginners (yes i tried google)?

  15. Brian says:

    Hi, I just found this tutorial and tried it on OS X 10.4.10.

    Here is what my username.conf file says:

    Options All
    AllowOverride All
    Order allow,deny
    Allow from all

    Mod_rewrite is still not working, though. What’s wrong? (Everything else in httpd.conf is uncommented)

  16. Brian says:

    Whoops that code is prefaced by

    and ends with

    (That’s all that’s in the brian.conf file)

  17. Heather says:

    THANK. YOU. SO. MUCH. I wasted so much time trying to figure this out myself. Thankyouthankyouthankyou.

  18. Matt K says:

    Brian, I have the same problem. 10.4.10, all of the lines were already uncommented and as described except the line at 408 (406 in my config) which I changed accordingly.

    As for my .htaccess file, I have the directives block the same as yours. I have checked everything I can think of, scoured the Apache documentation and searched all afternoon on the net for answers. This page was the closest I came and I’m still not quite there.

    If anyone has the answer for 10.4.10 it would be gratefully appreciated. If I find it, I’ll bring it back here! Cheers

  19. Matt K says:

    Sorted it. I was getting a “RewriteRule: bad flag delimiters” error in /private/var/log/httpd/error_log - removed the comment I had put in after the rewrite rule and suddenly everything worked. Cheers for being instrumental in getting this working for me everyone.

  20. Luigi says:

    Thanks Michael !
    It was what I needed this aftrnoon when I installed a local copy of the site on OSX.
    Thanks ! It’s perfect.

    I think you should also write a brief tutorial about installing PHP, mySQL and Wordpress on OSX, with your clear style.
    All the sources on the web are not clear or outdated…

    Luigi

  21. Michael says:

    Thanks, Luigi!
    I’m really glad it’s helped so many people so far. It’s hysterical that you just asked about a tutorial on installing PHP and MySQL because almost two years after I’d written this I stayed up late last night and wrote just what you’re asking for (without the wordpress portion). You can read it here.

  22. Luigi says:

    You’re right !
    Pleae forgive…I’ve just found your site, and did not notice that nice tutorial.

    Perfect !

    ;)

  23. [...] the release of Leopard last Friday, it appears the bulk of my previous tutorials are now taken care of out of the box. mod_rewrite is enabled by default, and a decent [...]

  24. orko says:

    You have no idea how hard I worked at getting this to work. I installed Drupal in three different machines and the clean URL option worked only in one of them. The other two machines refused to do it!
    I compared every file in these two machines and configured them according to Drupal instructions. Nothing. I was starting to believe that the only difference was that I had installed Apple’s Web Objects.
    The main problem is that people who write instructions write the instructions to themselves not thinking on how a non-knowledgeable person thinks (or does not think).

    THANK YOU!!

    By the way, PHPMYAFMIN is giving me the following alet:
    “Your PHP MySQL library version 5.0.24a differs from your MySQL server version 3.23.32. This may cause unpredictable behavior.”

    Any ideas??

    Cheers

  25. [...] getting mod_rewrite to work in Leopard. The solution for me was the second set of instructions on Michael Krol’s blog. His was intended for Tiger, but worked for me nonetheless. I didn’t need to edit the [...]

  26. at0g says:

    Thanks guys, I had the same issue as Midiman… Thanks all the way from New Zealand =)

  27. Senning says:

    If you’re having trouble because your webserver keeps looking for your ./Sites/ files at /Library/WebServer/Documents/, and you don’t need to access /Library/etc…, you can add DocumentRoot “/Users/[username]/Sites/” to “yourname.conf”.
    I’m assuming that it is nicer to put it in yourname.conf so other users won’t be redirected to the same, but I’m not an expert.

  28. paul says:

    For OS X 10.4.11, Got this working with Drupal in the user/Sites dir, but needed one extra step.

    – modifying the [user].conf will allow .htaccess in the [user]/Sites/[drupal dir] to override the setting.
    – this site though is http://localhost/~user/drupal dir]
    – therefore in the .htaccess within the drupal directory needs to define where its root is through a “RewriteBase”
    – uncomment and change to “RewriteBase /~[user]/[drupal dir]”

    hope this helps a few.

  29. david says:

    I think I did everything right but when i tried to pull up my php site i got this:
    You don’t have permission to access /~david/elgreco/index.php on this server.

    any idea what i may have done wrong?

    -david

Leave a Reply