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…
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:
$ sudo pico /etc/httpd/httpd.confLoadModule rewrite_module libexec/httpd/mod_rewrite.soAddModule mod_rewrite.cAllowOverride AllAccessFileName .htaccess$ sudo apachectl restartNOTE: 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:
Options All
AllowOverride All$ sudo apachectl restartYou 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.
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:
$ sudo pico /etc/httpd/httpd.conf
<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!
$ sudo pico /etc/hosts##
# 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
$ sudo apachectl restartNow, 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!
All comments are moderated before being displayed. Please keep it polite and on topic. Show who you are with a Gravatar.
My name is Phil Smith, I currently live in Leeds (West Yorkshire), and I design and build websites. I like coffee, Guinness and the calm before the storm.
And you look lovely today, by the way.
Strictly speaking, I'm not currently available for freelance work but I'm always on the look out for interesting projects to collaborate on, so drop me a line.
Be the first to comment.