DokuWiki

It's better when it's simple

User Tools

Site Tools


rewrite

This is an old revision of the document!


URL Rewriting

FIXME 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.

Instructions in plain English

Steps

  1. Go to Admin
  2. Open Configuration Manager
  3. Change Nice URLs option to .htaccess (use find, it's far down)
  4. Underneath it, check the option to “Use slash as namespace separator in URLs”
  5. Save
  6. Next, using an ftp or file manager, you need access to your /wiki/ folder that you created when creating your wiki.
  7. Inside, you'll find a file called .htaccess and .htaccess.dist. (if you don't see it, make sure you have hidden files visible. It may be an option you have while logging on depending on your hosting provider, or, simply create a file called .htaccess and upload it)
  8. inside the .htaccess file, paste the following (alternatively uncomment section in .htaccess.dist then rename to .htaccess):
RewriteEngine on
 
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

This will give you a format like www.myexample.com/wiki-article. If you have a different format for your address, like www.myexample.com/dokuwiki/wiki-article, then place this line right after RewriteEngine on

RewriteBase /dokuwiki

where /dokuwiki should be changed to the actual name of the folder you are using.

Further Details for the Technically Savvy

DokuWiki supports two methods for URL rewriting, enabled through the userewrite option. One relies on 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.

URL-Rewriting can be enabled at ?do=admin&page=config#_advanced or in the local conf/local.php file line that reads $conf['userewrite'] = N;. N is the number 0, 1, or 2. The default is zero without this line present. Follow the configuration instructions below for whichever option is chosen.

Option 1: web server

:!: Remember to set following in the Configuration Manager /start?do=admin&page=config

  • Use nice URLs: .htaccess
  • Use slash as namespace separator in URLs [x]

Otherwise rewrite rules will not be useful.

Apache

Rewriting URLs in Apache is done through the mod_rewrite module of Apache 1 or Apache 2.

DokuWiki comes with an .htaccess.dist file which contains the needed rewrite rules for mode 1.

Here is an example

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 may need to replace the /dokuwiki with whatever directory you use in your URL to get to the wiki. If your wiki appears at the top level of the domain (e.g. http://example.com points to the start page of the wiki then use the following line: RewriteBase /.

You may also need to rename the file from .htaccess.dist to .htaccess.

Apachectl status broken

Dokuwiki rewrite rule affect 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

404 not found error

You may need to do this.

IIS (or Microsoft Azure Web App)

Enable url rewriting and insert appropriate set of rules:

<rewrite>
<rules>
 
<rule name="rule 1C" stopProcessing="true">
	<match url="^_media/(.*)" />
	<action type="Rewrite" url="/lib/exe/fetch.php?media={R:1}" appendQueryString="true" />
</rule>
<rule name="rule 2C" stopProcessing="true">
	<match url="^_detail/(.*)" />
	<action type="Rewrite" url="/lib/exe/detail.php?media={R:1}" appendQueryString="true" />
</rule>
<rule name="rule 3C" stopProcessing="true">
	<match url="^_export/([^/]+)/(.*)" />
	<action type="Rewrite" url="/doku.php?do=export_{R:1}&amp;id={R:2}" appendQueryString="true" />
</rule>
<rule name="rule 4C" stopProcessing="true">
	<match url="^$" />
	<action type="Rewrite" url="/doku.php" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
           </conditions>
</rule>
<rule name="rule 5C" stopProcessing="true">
	<match url="(.*)" />
	<action type="Rewrite" url="/doku.php?id={R:1}" appendQueryString="true" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
</rule>
<rule name="rule 6C">
	<match url="^index.php$" />
	<action type="Rewrite" url="/doku.php" />
</rule>
</rules>
</rewrite>

If you are using Azure Web App, just put these lines under the <system.webServer> tags in web.config file (situated in wwwroot of your App).

IIS 7.0 or greater

IIS 7.0 and above can be extended with a the URL Rewrite module 2.0 available from Microsoft for both x86 and x64

Save the Apache rules above to a .htaccess file removing the “RewriteBase /dokuwiki” line. This will not be required as we will be adding the rules directly to the root of the wiki.

In IIS Manager select your Wiki folder, and under IIS select “URL Rewrite” (if you had Manager open before installing, a refresh will be required)

In the actions pane on the right hand side, choose “Import Rules”

For your configuration file, select the .htaccess file and Import, this should give you 6 converted rules.

Finally hit Apply in the Actions panel on the right hand side.

Configure your Wiki to use .htaccess rules for rewrite and you should now be seeing your URLs rewritten.

Nginx

See nginx documentation. In the following example, our server root is /var/www, and we extract dokuwiki to /var/www/wiki.

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 ^/(?!lib/)(.*) /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;
    }
}

Notes

If using https the HTTPS server variable has to be set to allow proper linking in dokuwiki. This can be done in the server section with:

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.

Option 2: DokuWiki

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 setting. However, new IIS versions seem to work with this.

Clean PHP session ID

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:

session.use_only_cookies=1

You can also try:

session.use_trans_sid=0

Also see

rewrite.1504021281.txt.gz · Last modified: 2017-08-29 17:41 by 81.133.88.36

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki