Photo Gallery back online
July 9th, 2009 at 03:53am
The photo gallery is online again, having been offline since I moved the site in April.
I encountered an interesting problem when I brought the gallery back online where none of the links to the various photo albums were working properly. Links to the various administrative pages were working fine, and all of the images were loading properly, but clicking on a link to an album (for the purposes of this example, we’ll work with the “Old Cooma Road Bridge Upgrade” album) resulted in the URL in the address bar changing, but the home page of the gallery being served up. So, for example, the page at http://photos.samuelgordonstewart.com/OldCoomaRoadBridgeUpgrade was producing the same content as the page at http://photos.samuelgordonstewart.com/
To make things more bizarre, no errors were being produced in the site’s error log, so I really had nothing to go on, in order to work out what was going on.
On the previous server, the photo gallery was located in the directory /var/www/vhosts/samuelgordonstewart.com/subdomains/photos/httpdocs/ and I had already corrected the paths listed in the configuration files to the path on the new server, which is /home/samuelgo/public_html/photos, and I have moved the Gallery between servers and paths before without encountering this problem.
When I had a closer look at the directory structure inside the “photos” directory, I noticed that there is no directory with the name “OldCoomaRoadBridgeUpgrade” (or any of the other album’s names, for that matter) and that they in fact live within the “albums” directory (for example, /home/samuelgo/public_html/photos/albums/OldCoomaRoadBridgeUpgrade), and that the Gallery software must, therefore, be using Apache‘s (the webserver software) mod_rewrite functions to use nice URLs such as http://photos.samuelgordonstewart.com/OldCoomaRoadBridgeUpgrade instead of some archaic (and ugly) URL such as http://photos.samuelgordonstewart.com/albums.php?set_AlbumName=OldCoomaRoadBridgeUpgrade
So, why was mod_rewrite failing to behave properly…time to check the .htaccess file at /home/samuelgo/public_html/photos/.htaccess which contains the mod_rewrite rules. Hmmm, that’s interesting, there are no mod_rewrite rules in it…so why am I getting the index page instead of a “404 not found” error when I visit something which physically doesn’t exist such as http://photos.samuelgordonstewart.com/OldCoomaRoadBridgeUpgrade?
Then it struck me, this is precisely the behaviour I would expect if WordPress‘ (the software which runs this blog) standard mod_rewrite rules were being followed…that is, if a given URL doesn’t physically exist, refer to index.php to find out what to serve up. None of the pages on this blog exist in physical files, they are all generated on request by the index.php (and others) file running database queries and extracting the required information out of the blog’s database. Gallery doesn’t work in quite the same way, and it definitely doesn’t understand the WordPress method.
But why was I getting WordPress’ mod_rewrite behaviour now, when I wasn’t getting it on the old server’s Gallery installation. Well that’s simple. The old server was running the Plesk control panel, which keeps primary domains separated from their subdomains. On the old server, this blog existed at /var/www/vhosts/samuelgordonstewart.com/httpdocs whilst the photo gallery existed at /var/www/vhosts/samuelgordonstewart.com/subdomains/photos/httpdocs. This server, however, uses the cPanel control panel which stores subdomains within the directory of the primary domain. So the blog exists at /home/samuelgo/public_html whilst the gallery lives at /home/samuelgo/public_html/photos.
The annoying thing about the way cPanel handles subdomains is that .htaccess files take effect recursively. This means that the .htaccess file which lives at /home/samuelgo/public_html/.htaccess has an affect on the public_html directory and all of the directories within it, and the directories within those directories, and so on and so forth. Annoyingly, this means that the mod_rewrite rules used by WordPress get applied to the photo gallery with disastrous results.
I was able to prove that this was the case by temporarily taking the blog’s .htaccess file out of commission, which resulted in virtually every link on the blog and the photo gallery producing a “404 not found” message.
So the problem now was how to get the Gallery installation to produce its mod_rewrite rules again. My theory was that if I disabled Gallery’s use of mod_rewrite, and then re-enabled it, Gallery would need to reproduce the mod_rewrite rules. Unfortunately I had already gone through the Gallery configuration wizard to see if there was anything I had missed (alas, there wasn’t) and noticed that using mod_rewrite is not optional. If mod_rewrite is installed on the server, Gallery will use it. This didn’t completely kill the idea though.
I decided to manually modify the configuration file which controls whether Gallery uses mod_rewrite, and tell it not to use mod_rewrite. This worked, as it forced Gallery to use archaic URLs such as http://photos.samuelgordonstewart.com/albums.php?set_AlbumName=OldCoomaRoadBridgeUpgrade which were working fine. I then ran the configuration wizard again, which noticed that mod_rewrite is installed on the server, and thus decided to enable Gallery’s use of mod_rewrite, and write the mod_rewrite rules as needed.
It wrote the following in to the photo gallery’s .htaccess file, and normal Gallery URLs started working again:
# BEGIN Gallery section
# (Automatically generated. Do not edit this section)
# Note: still under development, so format may change.
# If you edit this file, make a backup before runnng the Config. Wizard.Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.\?/]+)/([0-9]+)$ /view_photo.php?set_albumName=$1&index=$2 [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.\?/]+)/([A-Za-z_0-9\-]+)$ /view_photo.php?set_albumName=$1&id=$2 [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.\?/]+)/$ /$1 [R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.\?/]+)$ /view_album.php?set_albumName=$1 [QSA]
# END Gallery section. Add User changes below this line
Which effectively overrides the mod_rewrite rules of the WordPress installation for the “photos” directory and directories therein.
So that was my fun for the evening. And I apologise to the person who tried to access the “A Current Affair’s Ben Fordham rings 2CC’s Mike Welsh to take exception with something Mike said” article and instead got a “404 not found” page in the few moments that I had this blog’s .htaccess file out of commission.
Now, time to put the pictures of the construction of Googong Dam’s spillway online.
Samuel
Entry Filed under: Blog News