PHP
Setting Up PHP And Apache For RapidWeaver
2007-12-17

Clamshell Bucket: 1/800s f/4.0 ISO100 170mm -0.3ev, Canon 30D, Canon EF 70-200 f/2.7L IS, adjusted
RapidWeaver, the application I use to create this site, now has the option to generate the sidebar using PHP. I plan on taking advantage of this feature so that sidebar modifications (which includes adding new pages) don't affect all the HTML on the site and require a complete upload, 1000+ files in my case.
But switching to PHP creates a problem. Although the links I publish, such as http://www.bagelturf.com, don't specify the file extension and so will be automatically redirected to the appropriate index file, there are many links out there on the web that specify .html and those files will no longer exist. An example is: http://www.bagelturf.com/aparticles/index.html. When I replace this file with http://www.bagelturf.com/aparticles/index.php, all the links to the index.html file will be broken.
The fix to this is to use the mod_rewrite facilities of Apache. mod_rewrite allows rules to be set up that can generate new URLs from those that match patterns. The goal here is to convert all requests for .html into requests to .php. I found a couple of good sites to help me with this, here and here.
I want to experiment with PHP and mod_rewrite on my computer before making any changes on my hosting service. As it ships, Tiger has PHP disabled. I'm still running Tiger, note. The changes are slightly different for Leopard. To turn on PHP, I fired up TextWrangler and selected File > Open File By Name... This lets me type in the Apache configuration file name I need to change: /etc/httpd/httpd.conf. This file needs root access to edit, so as soon as I start to edit it, TextWrangler asks me if I really mean to do this. When I save the file it requests my password. I prefer using TextWrangler since I don't need to mess with the terminal or sudo.
To enable PHP I remove the # from the following two lines in httpd.conf:
# LoadModule php4_module libexec/httpd/libphp4.so
# AddModule mod_php4.c
Then I start Apache. This is done from the Sharing preference pane by turning on Personal Web Sharing:

If I already had Apache running, then I would have had to stop it and restart it by unchecking and then checking the checkbox.
I can check that PHP is active this by creating a simple PHP file and accessing it in my Sites folder in my home folder. The file is called index.php and looks like this:

To access it I load the URL http://127.0.0.1/~steve/index.php in a browser and expect to see a page with Hello World appear. It does, so now PHP is working.
To use mod-rewrite I create rules and put them into a .htaccess file in my Sites folder. To create the file, I again use TextWrangler and type in the following:

Options +FollowSymlinks is needed or else the rule does not work. The RewriteBase line tells Apache that my web pages live in my home folder. Without this line the access will go to the default web page for the system, which is not what I want. The RewriteRule line matches all URLs that end with .html and replaces the extension with .php. The Item as the end of the line tells Apache to give a response code of 301 (Moved Permanently) and to reload the page. This reload replaces the URL in the browser with the actual one I am using, the one ending with .php. This will tell search engines where my .html pages have gone.
There is one more step to getting this work. As set up so far, Apache will not parse .htaccess files in my Sites folder because that feature is turned off. As well as processing the global httpd.conf file when started up, Apache also parses a file users/steve.conf and by default that is set to deny access to .htaccess files.
To fix this I use TextWrangler to edit the file /etc/httpd/users/steve.conf so that it looks like this:

The edit needed is to change AllowOverride from None to All. This file applies specifically to my Sites folder and to everything in subfolders below that.
I'm done. Now I can experiment with my site by enabling PHP sidebar generation and viewing the pages locally. That's turned on in the Page Inspector:

|
The Bagelturf site welcomes Donations of any size