URL Rewriting

By default DokuWiki does no URL rewriting resulting in URLs like this: http://example.com/doku.php?id=page. These URLs are considered ugly and are not indexed well by some searchengines. The solution is to use URL rewriting. DokuWiki supports two methods for URL rewriting through the userewrite option. One utilizes the rewriting cababilities of the webserver the other one handles rewritten URLs inside DokuWiki. Below follows a table explaining all options again.

Value Info Example URL
0 No URL rewriting is used. This is the default. http://example.com/dokuwiki/doku.php?id=wiki:syntax
1 Rewriting is handled by the webserver. http://example.com/dokuwiki/wiki:syntax
2 Rewriting is done by DokuWiki. http://example.com/dokuwiki/doku.php/wiki:syntax

URL-Rewriting is disabled by default because it needs some additional configuration besides setting the appropriate config option - these configs are discussed below.

Option 1: Webserver

$conf['userewrite'] = 1;

This option produces the nicer URLs but needs a webserver configured to parse rewritten URLs and give DokuWiki the deconstructed URL.

Apache

Rewriting URLs in Apache is done through the mod_rewrite module of Apache 1 or Apache 2. The setup of this module is done through so called rewrite rules which can be either defined directly in the server's main config or in a .htaccess file located in DokuWiki's main directory.

DokuWiki comes with an .htaccess.dist file which contains the needed rewrite rules for mode 1, but commented. Just copy the file to .htaccess (in the folder that contains doku.php, caveat debian users) and uncomment the lines to enable rewriting.

RewriteEngine on

RewriteBase /dokuwiki

RewriteRule ^_media/(.*)              lib/exe/fetch.php?media=$1  [QSA,L]
RewriteRule ^_detail/(.*)             lib/exe/detail.php?media=$1  [QSA,L]
RewriteRule ^_export/([^/]+)/(.*)     doku.php?do=export_$1&id=$2  [QSA,L]
RewriteRule ^$                        doku.php  [L]
RewriteCond %{REQUEST_FILENAME}       !-f
RewriteCond %{REQUEST_FILENAME}       !-d
RewriteRule (.*)                      doku.php?id=$1  [QSA,L]
RewriteRule ^index.php$               doku.php

On the line RewriteBase /dokuwiki, you need to replace the /dokuwiki with whatever directory you use in your URL to get to the wiki. Say that your normal (Option 0) URL is http://www.whatever.com/projects/documents/doku.php . You will need to set the above line to RewriteBase /projects/documents. However sometimes this line is not needed at all.

Some Notes

.htaccess files are only honored if Apache's main config allows it. Many default Apache installs don't. To enable them try adding the following to the httpd.conf:

<Directory /path/to/dokuwiki>
  AllowOverride All
</Directory>

Alternativly you may simply specify the rewrite rules mentioned above directly in the httpd.conf:

<Directory /path/to/dokuwiki>
  RewriteEngine on
  ... rewrite rules here ...
</Directory>

You may have to restart Apache for these changes to work.

IIS

IIS doesn't come standard with a rewrite module. I used ISAPI Rewrite Lite (free) successful with these rewrite rules (see the file C:\Program Files\Helicon\ISAPI_Rewrite\httpd.ini):

# Dokuwiki rules
 
RewriteRule ^(/wiki/)_media/(.*)\?(.*) $1lib/exe/fetch.php\?media=$2&$3 [I,L]
RewriteRule ^(/wiki/)_detail/(.*)\?(.*) $1lib/exe/detail.php\?media=$2&$3 [I,L]
RewriteRule ^(/wiki/)_detail/(.*) $1lib/exe/detail.php\?media=$2 [I,L]
RewriteRule ^(/wiki/)_export/([^/]+)/(.*) $1doku.php?do=export_$2&id=$3 [I,L]
 
RewriteRule (/wiki/) $1doku.php [I,L]
 
RewriteRule ^(/wiki/)\?idx=(.*) $1doku.php\?idx=$2 [I,L]
RewriteRule ^(/wiki/)lib/(.*) $1lib/$2 [I,L]
RewriteRule ^(/wiki/)(.*)\?do=(.*) $1doku.php\?id=$2&do=$3 [I,L]
RewriteRule ^(/wiki/)doku.php\?id=(.*) $1doku.php\?id=$2 [I,L]
RewriteRule ^(/wiki/)(.*) $1doku.php\?id=$2 [I,L]
 
# this rule fixes a problem to see the old revisions
RewriteRule ^(/wiki/)(.*)\?(.*) $1doku.php\?id=$2&$3 [I,L]

For all lines with RewriteRule ^(/wiki/), you need to replace the (/wiki/) with whatever directory you use in your URL to get to the wiki. Say that your normal (Option 0) URL is http://www.whatever.com/projects/documents/doku.php . You will need to set the above line to ^(/projects/documents/).

Lighttpd

This is an improved version of one suggested for lighty.

url.rewrite-once = (                                                                                                   
                    "^(/|index.php)?$" => "/doku.php",                                                                     
                    "^/lib/(.*)/?$" => "/lib/$1",
                    "^/_media/(.*)?\?(.*)$" => "/lib/exe/fetch.php?media=$1&$2",
                    "^/_media/(.*)$" => "/lib/exe/fetch.php?media=$1",
                    "^/_detail/(.*)?\?(.*)$" => "/lib/exe/detail.php?media=$1&$2",
                    "^/_detail/(.*)?$" => "/lib/exe/detail.php?media=$1",
                    "^/_export/([^/]+)/(.*)$" => "/doku.php?do=export_$1&id=$2",
                    "^/(?!doku.php|feed.php|robots.txt|sitemap.xml.gz)(.*)\?(.*)/?$" => "/doku.php?id=$1&$2",
                    "^/(?!doku.php|feed.php|robots.txt|sitemap.xml.gz)(.*)/?$" => "/doku.php?id=$1",
                  )

Nginx

Nginx is a very fast and stable httpd, see more about nginx project, and an English wiki. In the following example, our server root is /var/www, and we extract dokuwiki to /var/www/dokuwiki.

    server {
        listen       80;
        server_name  _ *;
        port_in_redirect off;
        optimize_server_names off;

        access_log  /var/log/nginx/localhost.access.log;

        rewrite ^(/dokuwiki/)_media/(.*) $1lib/exe/fetch.php?media=$2 last;
        rewrite ^(/dokuwiki/)_detail/(.*) $1lib/exe/detail.php?media=$2 last;
        rewrite ^(/dokuwiki/)_export/([^/]+)/(.*) $1doku.php?do=export_$2&id=$3 last;

        location / {
            root   /var/www;
            index  index.html index.htm index.php;
        }

        location /dokuwiki/ {
            if (!-f $request_filename) {
                rewrite ^(/dokuwiki/)(.*)?(.*)  $1doku.php?id=$2&$3 last;
                rewrite ^(/dokuwiki/)$ $1doku.php last;
            }
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/www;
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:8888;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
            include        /etc/nginx/fastcgi_params;
        }
    }

Notes

Nginx has complete support for fastcgi, please reference nginx fastcgi document to fit your setup.

The last keyword of rewrite rules before location setup make sure that rewrite only happens once. You should replace all /dokuwiki/ appeared above to you wiki directory relative to web server root directory.

Option 2: DokuWiki

$conf['userewrite'] = 2;

This option won't need any webserver setup. However it relies on the PATH_INFO feature of the CGI standard as implemented by Apache. IIS is known not to work with this setting.