Apache mod_rewrite, PHP and OS X 10.6 Snow Leopard

Getting mod_rewrite to work on your mac (i.e. locally) can be a bit of a trick if you’ve never bothered to try before. This was the case with me just last week. I am in the process of rewriting my CMS for seyDesign which will rely heavily on $_GET queries. I still want users to be able to link to their favorite pages with easy-to-read and remember “flat” URL’s so I needed to be able rewrite links like this:

http://seydesign.com/index.php?page=themes&product=123

…so that they appear in the users address bar like this:

http://seydesign.com/themes/Creami3/

The best way to do this is with the Apache module, mod_rewrite. I won’t get into how exactly to use it here, but you should be able to use mod_rewrite both on your remote server and on your local machine. For instance, the above two examples, when viewed from the localhost would look like this:

http://localhost/index.php?page=themes&product=123

…which, for the viewer, converts to:

http://localhost/themes/Creami3/

Using mod_rewrite in MAMP, my preferred AMP stack for local development, was simple enough as MAMP is preconfigured to do so. But since I use MAMP for testing support cases, and since I knew that the new new seydesign build will be an on-going project, I decided to move it onto the built in web stack on Mac OS X 10.6 Snow Leopard, to give it a permanent home I could access at any time.

The trouble for me arose when I tried to use mod_rewrite on the built in OS X web stack. It seems that the permission to use mod_rewrite, or at least to set the rules from .htaccess, are forbidden. In addition, PHP is disabled by default (I can’t very well write $_GET queries without PHP).

Every resource I found online had me editing the /etc/apache2/httpd.conf file, which is all well and good until Apple comes along and updates the Apache install.

A better approach, I feel, is to make any changes to the user config file and leave the default httpd.conf alone. So the first thing I did was open this file (replace username with your user name):

/etc/apache2/users/username.conf

By default, its content should look like this:

<Directory "/Users/username/Sites/">
    Options Indexes MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

To get PHP running and mod_rewrite to work you need to make a few changes. For starters, if you notice in the /etc/apache2/httpd.conf file, this line is commented out:

# LoadModule php5_module libexec/apache2/libphp5.so

So let’s add that to out own config file and uncomment it, so our username.conf file now looks like this:

LoadModule php5_module        libexec/apache2/libphp5.so

<Directory "/Users/username/Sites/">
    Options Indexes MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

It will make our life easiest if we point to DocumentRoot our way as opposed to where the httpd.conf file points, /Library/WebServer/Documents. In my case, it’s /Users/username/Sites/build/seyDesign2011 but you can make it where ever you like. We also want to change our directory to match, so our username.conf file now looks like this:

LoadModule php5_module        libexec/apache2/libphp5.so

DocumentRoot "/Users/username/where/ever/you/like"

<Directory "/Users/username/where/ever/you/like">
    Options Indexes MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

To get mod_rewrite to work from .htaccess we need to do two more things; we need to add +FollowSymLinks or FollowSymLinks to our options and we need to change the AllowOverride setting to All.

Our username.conf file should now looks like this:

LoadModule php5_module        libexec/apache2/libphp5.so

DocumentRoot "/Users/username/where/ever/you/like"

<Directory "/Users/username/where/ever/you/like">
    Options Indexes MultiViews +FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

And that’s it. Once you have enabled web sharing in System Preferences > Sharing (or restarted apache from the command line, sudo apachectl restart), you are free to create your mod_rewrite rules to your hearts content from an .htaccess file served up on http://localhost/.

 

Adam Merrifield

 

One thought on “Apache mod_rewrite, PHP and OS X 10.6 Snow Leopard

Leave a Reply