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 search engines.
The solution is to enable URL rewriting, which is disabled by default. DokuWiki supports two methods for URL rewriting through the userewrite option. One utilizes the rewriting capabilities of the webserver; the other one handles rewritten URLs inside DokuWiki. The table below summarizes these options.
| 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 requires some additional configuration besides setting the appropriate config option - these configs are discussed below.
Rewriting URLs in Apache is done through the mod_rewrite module of Apache 1 or Apache 2. The module is enabled in httpd.conf with the following line (make sure it is not commented out):
LoadModule rewrite_module modules/mod_rewrite.so
On many kinds of Linux (Ubuntu, Debian, etc) you may enable mod_rewrite with:
sudo a2enmod rewrite
The setup of module mod_rewrite 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]
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.
.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 AuthConfig FileInfo Limit </Directory>
PS: The above will AllowOverride only what is needed for the default config to work. If it does not work for you, try using AllowOverride All
Alternatively 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.
Some users reported getting a “403 - Forbidden” Error after enabling the rewrite support. Setting the FollowSymlinks option seems to solve the problem:
Options +FollowSymlinks RewriteEngine on ...etc.
The rewrite rules given above will map all non-existing files and directories to the DokuWiki main dispatcher, this may apply to virtual mappings (aliases). Some host sites map web access statistics to a virtual /stats directory. To be able to still access these virtual directories, you need to exclude them in the rewrite conditions. Example:
... RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/stats/(.*)$ ...
If you're running without a RewriteBase, perhaps because you're hosting under a dedicated VirtualHost, you must modify the rewrite rules accordingly.
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 %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d RewriteRule (.*) /doku.php?id=$1 [QSA,L]
Each RewriteRule stanza must have a leading slash included, or Apache will return a 400 response code.
If you have a robots.txt and/or sitemap.xml file in your document root (for the Google bot) then you must exclude these files from rewriting in your .htaccess file:
RewriteCond %{REQUEST_FILENAME} !robots\.txt RewriteCond %{REQUEST_FILENAME} !sitemap\.xml
URL Rewrite Module is a Microsoft-supported rewrite module that can be installed on IIS 7 and newer. Starting from version 2.0 it has a wizard that will convert .htaccess rules to its own format (it uses standard IIS configuration mechanism which stores the site-specific settings into a web.config file in the root folder).
How to configure it for DokuWiki:
This procedure worked on my installation where DokuWiki was installed in the website root. I don't know for sure if these steps are enough if DokuWiki is installed in a subfolder (like /sample/documentation) but in principle, URL Rewrite module supports DokuWiki and for my scenario, it was able to convert .htaccess without any problems.
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 # Fix RSS Feeds RewriteRule ^(/wiki/)feed.php $1/feed.php [I,L] RewriteRule ^(/wiki/)feed.php\?(.*) $1/feed.php\?mode=$2&$3 [I,L] 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] # Diff still broken unless none is selected under 'use nice URL' options. You can still enter and link to nice URLs but the DokuWiki program will use normal naming.
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/).
It is also possible to use Ionics Isapi Rewrite Filter, which is free and open-source.
IterationLimit 1 # QSA is not supported, fix it RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule (.*)\?(.*) $1&$2 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 [QSA,L] # infinite redirect fix RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^/(.*)/$ /doku.php?id=$1 [QSA,L] # end fix RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^/(.*) /doku.php?id=$1 [QSA,L]
Assuming you have followed instructions for how to set up dokuwiki under lighttpd. If you haven't, define var.dokudir as shown in the example.
# rewrites for dokuwiki $HTTP["url"] =~ "^" + var.dokudir { index-file.names = ("doku.php") } url.rewrite = ( "^" + var.dokudir + "/lib/.*$" => "$0", "^" + var.dokudir + "/_media/(.*)?\?(.*)$" => var.dokudir + "/lib/exe/fetch.php?media=$1&$2", "^" + var.dokudir + "/_media/(.*)$" => var.dokudir + "/lib/exe/fetch.php?media=$1", "^" + var.dokudir + "/_detail/(.*)?\?(.*)$" => var.dokudir + "/lib/exe/detail.php?media=$1&$2", "^" + var.dokudir + "/_detail/(.*)?$" => var.dokudir + "/lib/exe/detail.php?media=$1", "^" + var.dokudir + "/_export/([^/]+)/(.*)\?(.*)$" => var.dokudir + "/doku.php?do=export_$1&id=$2&$3", "^" + var.dokudir + "/_export/([^/]+)/(.*)" => var.dokudir + "/doku.php?do=export_$1&id=$2", "^" + var.dokudir + "/doku.php.*" => "$0", "^" + var.dokudir + "/feed.php.*" => "$0", "^" + var.dokudir + "/(.*)\?(.*)" => var.dokudir + "/doku.php?id=$1&$2", "^" + var.dokudir + "/(.*)" => var.dokudir + "/doku.php?id=$1" )
Enable the rewrite module in lighttpd.conf by adding “mod_rewrite” in server.modules. An example config can be seen here.
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.
For NGINX 0.6.x
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) {
root /var/www;
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;
}
}
For later:
server {
listen 80;
server_name _ *;
port_in_redirect off;
optimize_server_names off;
access_log /var/log/nginx/localhost.access.log;
location / {
if (-f $request_filename) {
expires 30d;
break;
}
if (!-f $request_filename) {
rewrite ^/(.*)?(.*) /doku.php?id=$1&$2 last;
rewrite ^/$ /doku.php last;
break;
}
}
location ~ ^/_media/(.*)$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/doc/lib/exe/fetch.php;
fastcgi_param QUERY_STRING media=$1;
fastcgi_pass localhost:9000;
}
location ~ ^/dokuwiki/_detail/(.*)$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/doc/lib/exe/detail.php;
fastcgi_param QUERY_STRING media=$1;
fastcgi_pass localhost:9000;
}
# This one might be doable with split_path_info but this seems ok anyway
location ~ /dokuwiki/_export/([^/]+)/(.*)$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/doc/doku.php;
fastcgi_param QUERY_STRING do=export_$1&id=$2;
fastcgi_pass localhost:9000;
}
location ~ .php$ {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME /var/www/doc$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
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.
| $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 setting1). IIS6 (MS Server 2003) works pretty fine, when basedir is set to /.
Using Apache's Alias directive with this option can lead to severe headaches and broken wiki! ⇐ A patch which should solve this problem is available here: https://www.trg-oha.de/~sstrickroth/dokuwiki-alias.patch
Despite using “clean” URLs you may encounter a “DokuWiki” parameter in the URL looking like this:
PHP session ID:
http://example.com/example?DokuWiki=c81a95369a66576982119e2a60b557a5
This parameter is the PHP session ID and gets added by PHP automatically. It's completely unrelated to rewriting. To avoid it you can force PHP to always use cookies for sessions by setting the session.use_only_cookies option for PHP.
This usually done in the php.ini config file (when using a web hoster check their support page if and how you can edit these values):
session.use_only_cookies=1
If session id still work try:
session.use_trans_sid=0
When you PHP is used as Apache module you may be able to tweak these values using a .htaccess file using the following syntax:
php_flag session.use_only_cookies on php_flag session.use_trans_sid off
If you cannot use either of these solutions (many web hosts prevent editing php.ini and the use of php_flag in .htaccess), you will need to edit conf/local.php. Note that the above settings cannot be changed using ini_set() since PHP version 4.2.3, so the only way to do this is to use
ini_set('url_rewriter.tags', '');
My problem was that my session.cookie_domain was incorrectly set (I'm doing virtual hosting, and the domain name was incorrect). I addedphp_value session.cookie_domain www.my.domain.comto the appropriate virtualhost in my httpd.conf (though .htaccess would probably work just as well), and the polluted URLs disappeared. Also, if you can't get to your php.ini or apache config, you should be able to use ini_set('PHP variable', 'value'); to the same effect (it seems that conf/local.php is a good place to put this, as the file is included before the session is started).
In my case, FastCGI was causing session IDs to appear. Disabling it removed them.
basedir is set. — BlackFog 2007-12-19 15:42basedir no matter which system we are using (Apache or IIS, etc); even when doku.php is just in the root dir we still need to specify basedir as / — Yihui Xie 2008/05/28 20:09…/wiki/doku.php/… spread through the internet. If now I'll use .htaccess rewrite method (1), which is discussed here, all those old links with doku.php will become invalid. Is it possible to add some rules so that old links were still valid? If yes, please, explain how can it be done. RewriteRule doku.php/(.*) doku.php?id=$1 [QSA,L] right after RewriteRule ^_export/([^/]+)/(.*) doku.php?do=export_$1&id=$2 [QSA,L] line.[error] 27613#0: *1 open() "/usr/local/nginx/html/wiki/lib/tpl/arctic/images/button-firefox.png" failed
location /wiki/lib/ {
root /var/www/path/to/site.../;
}
— thedimi.net 2009-03-06 00:00
# Dokuwiki rules # Fix RSS Feeds RewriteRule ^(/)feed.php $1/feed.php [I,L] RewriteRule ^(/)feed.php\?(.*) $1/feed.php\?mode=$2&$3 [I,L] RewriteRule ^(/)_media/(.*)\?(.*) $1lib/exe/fetch.php\?media=$2&$3 [I,L] RewriteRule ^(/)_media/(.*) $1lib/exe/fetch.php\?media=$2 [I,L] RewriteRule ^(/)_detail/(.*)\?(.*) $1lib/exe/detail.php\?media=$2&$3 [I,L] RewriteRule ^(/)_detail/(.*) $1lib/exe/detail.php\?media=$2 [I,L] RewriteRule ^(/)_export/([^/]+)/(.*) $1doku.php?do=export_$2&id=$3 [I,L] RewriteRule (/) $1doku.php [I,L] RewriteRule ^(/)\?idx=(.*) $1doku.php\?idx=$2 [I,L] RewriteRule ^(/)lib/(.*) $1lib/$2 [I,L] RewriteRule ^(/)(.*)\?do=(.*) $1doku.php\?id=$2&do=$3 [I,L] RewriteRule ^(/)doku.php\?id=(.*) $1doku.php\?id=$2 [I,L] RewriteRule ^(/)(.*) $1doku.php\?id=$2 [I,L] # this rule fixes a problem to see the old revisions RewriteRule ^(/)(.*)\?(.*) $1doku.php\?id=$2&$3 [I,L] # Diff still broken unless none is selected under 'use nice URL' options. You can still enter and link to nice URLs but the DokuWiki program will use normal naming.