mod_rewrite and vhosts on OS X 10.4 (Tiger)

I’ve been unashamedly geeking it up this weekend, messing about with CodeIgniter, the PHP application framework from EllisLab. One of the great things about using a framework like this is that it automatically supports clean URLs (ie URLs without ?query=strings at the end) when used with the mod_rewrite feature of Apache…

Activating mod_rewrite:

The first thing that I discovered is that Tiger has introduced an extra level of confusion to the stock configuration of Apache. In addition to the httpd.conf file in the /etc/httpd directory, there’s also a users directory as well which holds unique config files for each user of the machine. So, if you were able to enable mod_rewrite or AllowOverrides in httpd.conf, you may find that it doesn’t work in your personal Sites directory. So let’s take a look…

In Terminal:

  1. Open /etc/httpd/httpd.conf:
    $ sudo pico /etc/httpd/httpd.conf
  2. Go to line 223 (or there abouts) and uncomment the following line:
    LoadModule rewrite_module libexec/httpd/mod_rewrite.so
  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. Save and exit the file, and then restart Apache:
    $ sudo apachectl restart

NOTE: I’m running a custom installed build of PHP5 from Marc Liyanage which had already amended my httpd.conf file to make most of these changes, but I though I’d include them anyway.

At this point you should have mod_rewrite happily fixing your ugly URL’s in the /Library/WebServer/Documents directory, but I’m guessing that it’s still not working in your /Users/you/Sites directory:

  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. And that’s about it.
If, like me, you run multiple sites out of sub-directories of your Sites directory, then you may notice that all hell is breaking loose in directories that have a .htaccess file trying to rewrite your URL’s. This is because the .htaccess file is redirecting everything back to the root of the Sites directory.

Enabling Virtual Hosts:

Here’s how to develop as many sites as you want, with “root relative” calls for graphics, scripts, CSS, links, etc. and keep all of the files for any particular site together - and they don’t even have to be in the default web server document root.

The practice is called “Name-Based Virtual Hosts” and is described in the Apache documentation (for those of you who want to read up on it). Essentially, I’ve managed to boil it down to a two(ish) step process…

In Terminal:

  1. Open back up your /etc/httpd/httpd.conf file:
    $ sudo pico /etc/httpd/httpd.conf
  2. Find the line that reads ### Section 3: Virtual Hosts and read the description below. Underneath the sample VirtualHost you’ll want to create the following hosts…

    <VirtualHost *:80>
    DocumentRoot /Library/WebServer/Documents
    ServerName localhost
    </VirtualHost>
    
    <VirtualHost *:80>
    DocumentRoot /Users/yourname/Sites/site/root/
    ServerName your.vhost.name
    </VirtualHost>

    An example from my own machine is a vhost for my installation of phpMyAdmin which looks like this:

    <VirtualHost *:80>
    DocumentRoot /Users/philsmith/Sites/php/phpMyAdmin/
    ServerName local.db
    </VirtualHost>

    As you can see, ‘your.vhost.name’ can be anything you like!

  3. Save and exit.
  4. Next you’ll need to edit /etc/hosts:
    $ sudo pico /etc/hosts
  5. It should look a little something like this:
    ##
    # Host Database
    #
    # localhost is used to configure the loopback interface
    # when the system is booting.  Do not change this entry.
    ##
    127.0.0.1       localhost
    255.255.255.255 broadcasthost
    ::1             localhost
    

    Below the first localhost description, add your new vhost. For my example above, this would be:

    127.0.0.1       local.db
  6. Save, exit and restart Apache:
    $ sudo apachectl restart

Now, if you go to http://your.vhost.name in your browser, you should see your site. If not, you may want to try:
$ sudo kill -HUP cat /var/run/lookupd.pid
$ sudo lookupd -flushcache

Magic!

Be the first to comment.

  • Required
  • Required but never published

All comments are moderated before being displayed. Please keep it polite and on topic. Show who you are with a Gravatar.