mertner.com/allan

Adding static pages to mertner.com

Tags: — Allan @ 12:25 am

The one thing that [WordPress](http://www.wordpress.org) does not do is deal with static or almost-static pages. I would like this to deal with things like contact details, a brief about-myself thing, my CV, etc.

Fortunately, a lot of people have written some excellent plugins and one of them does just what is required: it can turn a WordPress post into an almost static page that can be managed through the same interface as the rest of the blog. Enter the [Faked Folder](http://stevarino.com/wp/faked-folders/) plugin!

Using Faked Folders, this page now has links to somewhat static pages like [Contact](http://www.mertner.com/allan/about/contact) and [About]((http://www.mertner.com/allan/about/), which even have decent URLs. A “decent URL” in my book is one you can tell your mum to go to because it’s easy to remember and doesn’t have a not of ?/&%$#@ characters and numbers in them.

Just to complicate matters though, the nice URLs are achieved through URL rewriting. URL rewriting is a very funky feature that is called [mod\_rewrite](http://httpd.apache.org/docs/mod/mod\_rewrite.html) in Apache – it allows you to do a lot but it’s not stratightforward to understand how to get it to do what you want.

Enabling it for Fake Folders in WordPress is trivial:
1. Enable URL rewriting by adding RewriteEngine On in Apache’s configuration file for your site.
2. Create an empty .htaccess file in the directory containing the WordPress installation.
3. Add a line containing RewriteEngine On to the .htaccess file.

Figuring out that step 3 above was required took me a while – but once it was done, it worked beautifully because Fake Folders takes care of writing the really complicated stuff to the .htaccess file.

Multiple sites

Of course, when something looks easy, it’s only because there is something you have overlooked. The problem this time is the fact that [mertner.com](http://www.mertner.com) runs multiple sites off the same physical directory structure. Thus, if each site uses URL rewriting to achieve some nice static links, the contents of the .htaccess file is overwritten when it shouldn’t be – and Fake Folders does not cater for this.

Fortunately, looking at the .htaccess file for a site with a contact url makes it is easy to devise a fix:

RewriteEngine On
# BEGIN FAKED FOLDERS RULES
RewriteRule ^contact/?$ \
/index.php?static=true&p=1 [QSA]
# END FAKED FOLDERS RULES

By simply changing /index.php... to index.php..., the rule will work correctly for both the main site and the virtual sub-sites. If you don’t get any of this, once again I recommend the excellent (if somewhat dry) manual available on [mod\_rewrite](http://httpd.apache.org/docs/mod/mod\_rewrite.html).

Additionally, if the BEGIN FAKED... strings (which obviously are used to determine which lines Fake Folders can edit) could be changed to be user-specific and the rules be made user-specific as well, the entire problem is solved. Again, once the problem has been looked at, the solution is simple.

I modified the code to faked-folders.php in the modrewrite\_value() function to start something like this:

function modrewrite_value() {
  $folders = $this->folders;
  $user = get_virtual_user();
  $output = "# BEGIN FAKED FOLDERS RULES $user\n";
  $end = "# END FAKED FOLDERS RULES $user";
  if( ! $this->mod_rewrite ) {
    return $output."\n# CURRENTLY DISABLED\n\n$end";
  }

  if ($user == "/")
    $condition = "";
  else
    $condition = "RewriteCond %{REQUEST_URI} ^/".$user."\n";

This calls the code in [my-functions.php](http://www.mertner.com/sitecode/my-functions.phps) I wrote about in a previous post, where it retrieves the current site user. It then adds the user name to the strings, and adds a RewriteCond condition checking the URL prefix for rules that apply just to a single user.

Just a couple more steps were necessary:
1. Prepend the $condition variable to the $output variable all 3 places in the foreach loop.
2. Modify the try\_modrewrite function to also call get\_virtual\_user and append the user name to the BEGIN FAKED strings.

Every user on the system can now use rewritten URLs to go to semi-static pages. It’s great stuff! For reference, here is the Updated Faked Folders source code looks like, and this is what a “real” .htaccess file looks like for 2 users with just a couple of folders:

ReWriteEngine On

# BEGIN FAKED FOLDERS RULES allan
RewriteCond %{REQUEST_URI} ^/allan
RewriteRule ^about/([^/]+)/?$ \
index.php?static=true&name=$1 [QSA]
RewriteCond %{REQUEST_URI} ^/allan
RewriteRule ^about/?$ \
index.php?static=true&p=1 [QSA]
# END FAKED FOLDERS RULES allan

# BEGIN FAKED FOLDERS RULES morten
RewriteCond %{REQUEST_URI} ^/morten
RewriteRule ^contact/?$ \
index.php?static=true&p=11 [QSA]
# END FAKED FOLDERS RULES morten

Note: To avoid overflowing badly off the right of the screen, I have added line continuation characters (\) on the RewriteRule lines. The script generates those on a single line.

Static template

The one outstanding issue was the look of the static pages. [Faked Folder](http://stevarino.com/wp/faked-folders/) includes a mechanism for using a number of different templates (index.php variants, basically) for showing them in different ways.

This is cool, since it allows me to modify the “full” template and take out stuff I don’t want displayed on the static pages. This includes things like “Posted by”, the date, the comment link, etc.

Unfortunately, the mechanism is also a bit cumbersome. Every time I change something in the main site’s index.php, I have to make a few modifications to it and store it in the plugin setup for each of the 4 sites. This gets old quickly, and instead I now use a custom CSS for the static pages.

The static CSS is very similar to the standard one, except that several elements are marked as “not displayed” using this syntax:

.post .feedback, .postdate {
    display: none;
}

Now, the only difference between the standard index.php and the one used by the plugin is the stylesheet. I’ll think of a way of doing automatic stylesheet switching for the static pages, I think!

Addition 7 Sep 2004: A problem I have recently discovered is that the Faked Folders templates do not contain the code to include wp-blog-header.php. This is normally included to do all kinds of nice things for you when a page is displayed.

In fact, if you load an index.php file into the plugin and tells it to use it as its template, Faked Folders kindly edits out this reference. All well and good – it seems to work without it.

That is, until you decide to use a customized permalink structure that includes the category names instead of index.php?cat=17, which I just don’t find that appealing.

If you make this change, you need to include a call to update the category table in your Faked Folder templates – fortunately, it’s easy. Just add the following line early in the template:

  <?php update_category_cache(); ?>

With this change, even your customized permalink structure should work fine. Like on this site 🙂