How to Configure DokuWiki Clean URLs on Nginx for SEO

dokuwikinginx rewrite rulesdokuwiki seoclean urlsnginx configuration
Published·Modified·

DokuWiki is an open-source wiki engine that runs on a PHP environment. It is lightweight, powerful, and flexible, making it suitable for knowledge base management for small to medium-sized teams and personal websites. Recently, I started using this program and found it very simple and convenient. For SEO purposes, the first step is usually to set up clean URLs (pseudo-static). So, how do you configure DokuWiki on an Nginx web server?

Below is my personal Nginx vhost configuration file. You can add the rewrite rules to your Nginx configuration for reference:

server {
    listen 80;
    server_name zhuji.wiki www.zhuji.wiki;
    access_log /data/wwwlogs/zhuji.wiki_nginx.log combined;
    index index.html index.php duku.php;
    include /usr/local/nginx/conf/none.conf;
    root /data/wwwroot/zhuji.wiki;

    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(/|$) {
        #fastcgi_pass remote_php_ip:9000;
        fastcgi_pass unix:/dev/shm/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }

    location ~ .*\.(js|css)?$ {
        expires 7d;
        access_log off;
    }
}

The core rules that need to be added within the server block are:

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 ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
}

Next, add the following line to conf/local.php:

$conf['userewrite'] = 2;

Finally, modify the settings in the DokuWiki backend: go to Admin -> Configuration Manager -> Advanced Settings, select Use cleaner URLs, save the .htaccess file, and check the results.\n DokuWiki Clean URL Settings

For detailed rules, please refer to the official documentation: DokuWiki Rewrite.