/usr/share/dokuwiki/.htaccess
links to /etc/dokuwiki/htaccess
. So there is no caveat anymore.This is an old revision of the document!
This page is a mess and should be rewritten.
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.
Nota Bene In all the following examples, add or modify the line in your conf/local.php file so it reads:
$conf['userewrite'] = N; |
where N is the number 1 or 2. (No need to do anything if you want the option 0.)
This is not about URL rewrite but when you need to replace the “doku.php” for local reasons (it doesn't sound so well in all languages). I performed it with Apache server but I think it is similar or even the same with others.
header("Location: start.php");
inc/preload.php
is not part of default dokuwiki, so it won't be overwritten by a dokuwiki upgrade. <?php if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','start.php');
That's all!
If you find one more php file to edit, note it here, please.
Regards Bronek
Remember to set following in the Configuration Manager /start?do=admin&page=config
Otherwise rewrite rules will not be useful.
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 users1)) 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.
.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/(.*) %{DOCUMENT_ROOT}/lib/exe/fetch.php?media=$1 [QSA,L] RewriteRule ^/_detail/(.*) %{DOCUMENT_ROOT}/lib/exe/detail.php?media=$1 [QSA,L] RewriteRule ^/_export/([^/]+)/(.*) %{DOCUMENT_ROOT}/doku.php?do=export_$1&id=$2 [QSA,L] RewriteRule ^/$ %{DOCUMENT_ROOT}/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.
You may need to change
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
If the URLs are still not rewriting correctly, double-check that this setting is actually set correctly. In the config settings page, check that “Use nice URLs” is set to “.htaccess” in your config settings. Or, double-check that $conf['userewrite'] is set to '1' in conf/local.php .
Dokuwiki rewrite rule effect apachectl status command and make it return dokuwiki 404 page instead of the server-status page. You can fix that by either putting this in dokuwiki rewrite rules
RewriteCond %{REQUEST_URI} !^/server-status$
or creating an empty server-status file in dokuwiki root folder where doku.php is located. see forum post Apachectl status is broken with dokuwiki
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. It also works if Dokuwiki is installed in a subfolder.
IIS 6 and below don'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 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.
# We assume here that the wiki is accessible via http://wiki.example.com/dokuwiki/ var.dokudir = "/dokuwiki" # 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.7.65 or later ( config source )
server { server_name wiki.domain.tld; root /var/www/dokuwiki; location / { index doku.php; try_files $uri $uri/ @dokuwiki; } location @dokuwiki { rewrite ^/_media/(.*) /lib/exe/fetch.php?media=$1 last; rewrite ^/_detail/(.*) /lib/exe/detail.php?media=$1 last; rewrite ^/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last; rewrite ^/(.*) /doku.php?id=$1 last; } location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/tmp/phpcgi.socket; } }
For NGINX 0.6.x or later (tested up to 0.7.65)
server { listen 80; server_name example.com www.example.com ; #maximum file upload size is 4MB - change accordingly if needed client_max_body_size 4M; client_body_buffer_size 128k; root /var/www/wiki/; index index.html index.php; location / { if (-f $request_filename) { break; } if (!-f $request_filename) { #dokuwiki is installed in the root of the site rewrite ^/(.*)?(.*) /doku.php?id=$1&$2 last; rewrite ^/$ /doku.php last; break; } } #dokuwiki is installed in the root of the site rewrite ^/_media/(.*) /lib/exe/fetch.php?media=$1 last; rewrite ^/_detail/(.*) /lib/exe/detail.php?media=$1 last; rewrite ^/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last; location ~ \.php$ { fastcgi_pass phpfcgi; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
For nginx 0.7.27 or later (with try_files support)
server { listen 80; server_name example.com www.example.com; #maximum file upload size is 4MB - change accordingly if needed client_max_body_size 4M; client_body_buffer_size 128k; root /var/www/wiki; index doku.php; location / { try_files $uri $uri/ @dokuwiki; } location @dokuwiki { rewrite ^/_media/(.*) /lib/exe/fetch.php?media=$1 last; rewrite ^/_detail/(.*) /lib/exe/detail.php?media=$1 last; rewrite ^/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last; rewrite ^/(.*) /doku.php?id=$1&$args last; } location ~ \.php$ { if (!-f $request_filename) { return 404; } include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param REDIRECT_STATUS 200; fastcgi_pass 127.0.0.1:9000; } }
For nginx 0.7.27 or later (with try_files support & dokuwiki is installed in the root of the site)
server { listen 80; root /var/www; server_name yourdomain.net; location / { try_files $uri $uri/ @dk; rewrite ^/$ /doku.php last; } location @dk { rewrite ^/(.*) /doku.php?id=$1 last; } rewrite ^/_media/(.*) /lib/exe/fetch.php?media=$1 last; rewrite ^/_detail/(.*) /lib/exe/detail.php?media=$1 last; rewrite ^/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last; include common.conf; # other settings like php-fpm. }
Nginx has complete support for fastcgi, please reference nginx fastcgi document to fit your setup.
If using https the HTTPS server variable has to be set to allow propper linking in dokuwiki. This can be done in the server section like this:
server { listen 443; ... location ~ .php$ { ... include fastcgi_params; ## sets $_SERVER['HTTPS'] fastcgi_param HTTPS on; }
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.
If you are copy-pasting these configs, make sure you are adding locations to secure some of the directories as described on Security page.
In your conf/local.php file, add or modify the line so it reads:
$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 setting2). 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 is 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 your 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.
location /wikifolder/ { index doku.php; try_files $uri $uri/ @namethisanything; } location @namethisanything { rewrite ^/wikifolder/_media/(.*) /wikifolder/lib/exe/fetch.php?media=$1 last; rewrite ^/wikifolder/_detail/(.*) /wikifolder/lib/exe/detail.php?media=$1 last; rewrite ^/wikifolder/_export/([^/]+)/(.*) /wikifolder/doku.php?do=export_$1&id=$2 last; rewrite ^/wikifolder/(.*) /wikifolder/doku.php?id=$1 last; }
I don't know if omitting the location ~ \.php$ { block from the example above will cause any problems but things seem ok so far…
RewriteRule ^/\?(.*) /doku.php?$1 [QSA,L]
underneath the _export
rule.$conf['userewrite'] = 1;
in my conf/local.php file. I almost erased my Dokuwiki installation because of those two little missing details. — bstp 2010-11-26
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
My solutions is to add the following to the nginx.conf:
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.
example.com/
instead of example.com/dokuwiki/
. The problem is all links are rewritten back to example.com/dokuwiki/samplepage
and if I set basedir to /
then css and javascript is broken. I can't figure out how to modify my htaccess to get it to work without moving dokuwiki files to root directory./usr/share/dokuwiki/.htaccess
links to /etc/dokuwiki/htaccess
. So there is no caveat anymore.