Simon Heimlicher
 
  
 

Wiki Farm

This page explains how to run multiple DokuWiki sites with just one set of DokuWiki code.

Use at your own risk.

This is a work in progress with known flaws and bugs.

index.php in DocumentRoot of private virtual host private.example.com

<?php
/*
define('SH_FARM_DEBUG',1);
function _shdebugfarm($str) {
    if (defined('SH_FARM_DEBUG')) {
        print "<p>SH_FARM_DEBUG: ".$str."</p>\n";
    }
}
*/
 
define('DOKU_INC','/var/www/pkg/dokuwiki-stable.git/');
define('DOKU_CONF', realpath(dirname(__FILE__).'/../dokuwiki/').'/conf/');
 
$reqfile = 'doku.php';
 
if (isset($_REQUEST['reqfile'])) {
    $reqfile = $_REQUEST['reqfile'];
}
 
/*
_shdebugfarm("GET:\n".print_r($_GET,true));
_shdebugfarm("REQUEST:\n".print_r($_REQUEST,true));
_shdebugfarm("SERVER:\n".print_r($_SERVER,true));
_shdebugfarm('DOKU_INC='.DOKU_INC."\nDOKU_CONF=".DOKU_CONF);
 */
 
$file = DOKU_INC.$reqfile;
 
if (preg_match('{^([-a-z0-9_]+/)*([-a-z0-9_]+)\.(png|jpg|jpeg|pdf)$}i', $reqfile)) {
 
 
    $ftype = 'application/octet-stream'; 
/*
    $finfo = @new finfo(FILEINFO_MIME); 
    $fres = @$finfo->file($file); 
*/
    $fres = mime_content_type($file);
    if (is_string($fres) && !empty($fres)) { 
        $ftype = $fres; 
    } 
 
    //_shdebugfarm("file=$file, ftype=$ftype");
    header("Content-Type: $ftype");
 
    // Use X-Sendfile header to pass the delivery to Apache
    header("X-Sendfile: $file");
    ob_end_clean();
    exit;
 
    // Send file contents
    //readfile($file);
    //exit;
}
 
//_shdebugfarm("require_once '".$file."'");
if (is_file($file)) {
    include(DOKU_INC.$reqfile);
}
else {
    /*
    header("HTTP/1.0 404 Not Found", true, 404);
    print "<html><body>Not Found</body></html>";
    */
 
    /* Redirect */
    $host  = $_SERVER['HTTP_HOST'];
    if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
        $hosts = explode(',', $_SERVER['HTTP_X_FORWARDED_HOST']);
        $host = $hosts[0];
    }
    $uri_up   = preg_replace('{/(:?[^/]+|[^/]+/)$}', '', $_SERVER['REDIRECT_URL']);
    //_shdebugfarm("Location: http://$host".$uri_up."/");
    header("Location: http://$host".$uri_up."/");
    exit();
}

.htaccess file in DocumentRoot of private virtual host private.example.com

Order allow,deny
Allow from example.com

AuthName "Simon Heimlicher"
AuthType Basic
AuthUserFile /var/www/auth/htpasswd
AuthGroupFile /var/www/auth/htgroup
Require valid-user

Satisfy any

RewriteEngine on

# Not all installations will require the following line.  If you do, 
# change "/" to the path to your dokuwiki directory relative
# to your document root, e.g.
#RewriteBase /dokuwiki

RewriteBase /


RewriteRule ^index.php$                 -  [QSA,L]
RewriteRule ^$                          index.php  [QSA,L]

# DokuWiki

# Enable logout based on query string `do=logout'
RewriteCond %{QUERY_STRING}             do=logout
RewriteRule (.*)                        http://example.com/$1?do=logout [redirect=permanent,last]


# General rewrite rules for DokuWik farm

# lib/exe: executable PHP files
# lib/plugins: image files
# lib/tpl: templates, i.e. executable PHP files and image files
# lib/plugins: plugins, i.e. executable PHP files and image files
RewriteRule ^(lib/(exe|images|tpl|plugins|scripts)/.*)  index.php?reqfile=$1 [QSA,L]

RewriteRule ^_media/(.+)                index.php?reqfile=lib/exe/fetch.php&media=$1  [QSA,L]
RewriteRule ^_detail/(.+)               index.php?reqfile=lib/exe/detail.php&media=$1  [QSA,L]
RewriteRule ^_export/([^/]+)/(.*)       index.php?reqfile=doku.php&do=export_$1&id=$2  [QSA,L]

RewriteCond %{REQUEST_FILENAME}         !-f
RewriteCond %{REQUEST_FILENAME}         !-d
RewriteRule (.*)                        index.php?reqfile=doku.php&id=$1  [QSA,L]

.htaccess' in DocumentRoot of public virtual host example.com'':

RewriteEngine On

# DokuWiki

# General rewrite rules for DokuWik farm

# lib/exe: executable PHP files
# lib/plugins: image files
# lib/tpl: templates, i.e. executable PHP files and image files
# lib/plugins: plugins, i.e. executable PHP files and image files
RewriteRule ^(lib/(exe|images|tpl|plugins|scripts)/.*)                        http://private.example.com/$1 [proxy,qsa,last]

# Redirect back to private.example.com after editing a page
# (DokuWiki itself redirects POST to doku.php to DOKU_URL, which is set to example.com)
# Exclude HTTP/1.0, which does not have a REFERER field
RewriteCond %{HTTP_REFERER}             .
RewriteCond %{HTTP_REFERER}             http://private\.example\.com/
RewriteCond %{QUERY_STRING}             !do=logout
RewriteCond %{REQUEST_URI}             !do=logout
RewriteRule (.*)                        http://private.example.com/$1 [redirect=permanent,qsa,last]

# Rewrite remaining requests with query string 
# (i.e. remove query string by appending a blank `?')
RewriteCond %{QUERY_STRING}             .
RewriteCond %{QUERY_STRING}             !do=logout
RewriteRule (.*)                        http://example.com/$1? [redirect=permanent,last]

# Login to DokuWiki
RewriteRule (.*?)/?login$                  http://private.example.com/$1 [redirect,qsa,last]
RewriteRule (.*?)/?edit$                  http://private.example.com/$1?do=edit [redirect,qsa,last]

# Note: allow "HEAD" for agents and spiders like wget
RewriteCond %{REQUEST_METHOD}       ^GET|HEAD$
RewriteCond %{REQUEST_URI}          !\.php
RewriteRule ^(.*)$                  http://private.example.com/$1? [proxy,last]

# Allow method POST for discussion plugin
RewriteCond %{REQUEST_METHOD}       ^POST$
RewriteCond %{REQUEST_URI}          doku.php
#RewriteRule (.*)                    http://private.example.com/?reqfile=$1 [proxy,last]
RewriteRule (.*)                    http://private.example.com/$1 [proxy,last]

# Allow lib/plugins/captcha/{img|wav}.php for CAPTCHA plugin
RewriteCond %{REQUEST_METHOD}       ^GET|HEAD$
RewriteCond %{REQUEST_URI}          lib/plugins/captcha/img.php [OR]
RewriteCond %{REQUEST_URI}          lib/plugins/captcha/wav.php
#RewriteRule ^(.*)$                  http://private.example.com/?reqfile=$1? [proxy,qsa,last]
RewriteRule ^(.*)$                  http://private.example.com/$1 [proxy,qsa,last]

RewriteRule (.*)                    - [F]

Discussion

Enter your comment
XUQND