Adding static pages to

The one thing that [WordPress]( 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]( plugin!

Using Faked Folders, this page now has links to somewhat static pages like [Contact]( and [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](\_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 []( 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
RewriteRule ^contact/?$ \
/index.php?static=true&p=1 [QSA]

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](\_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 = "";
    $condition = "RewriteCond %{REQUEST_URI} ^/".$user."\n";

This calls the code in [my-functions.php]( 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

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]

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

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]( 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 🙂

2 thoughts on “Adding static pages to

  1. if you’re using WP 1.3-alpha, then you can create semi-static page ‘About’ and then add this line in your .htaccess

    RewriteRule ^about/? /wp/index.php?pagename=about [QSA]

    Voila, you have /about !

Comments are closed.