[Ubuntu] Dokuwiki Nice URL(rewrite) 설정

[Ubuntu] Dokuwiki Nice URL(rewrite) 설정
Photo by Remotar Jobs / Unsplash

사용환경

  • OS : Ubuntu 22.04 LTS (Jammy Jellyfish)
  • Apache : 2.4.52
  • PHP : 8.1.2
  • Dokuwiki : Release 2024-02-06a "Kaos"

A. 시작하기 전에..

dokuwiki를 설치할 때마다 항상 이 apache의 rewrite 설정을 헤매는 것 같다. 방금 설정을 완료해서 까먹기 전에 기록용으로 적어본다.

B. Nice URL(rewrite)란?

간단하게 이야기하면 URL 변환기능이다. dokuwiki를 기준으로 원본 URL은 rewrite할 경우에 더 깔끔하게 URL이 바뀐다.

원본URL : http://wiki.simplism.local/doku.php?id=start
변환URL : http://wiki.simplism.local/start

doku.php?id= 부분은 사실 사용자(=나) 입장에서는 굳이 알 필요가 없는 내용이다. dokuwiki의 가장 상위 페이지인 start 화면을 호출하는데에 doku.php?id= 를 빼고 start만 입력하면 얼마나 깔끔한가? (그래서 기능의 이름이 Nice URL 인가보다)

C. Rewrite 모듈적용

아마도 apt install apache2 명령어를 이용해서 apache를 설치했다면, rewrite 모듈이 설치는 되어있을 것이다. 아래의 명령어를 이용해서 rewrite 모듈을 apache2에 적용해보자.

$ sudo a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
  systemctl restart apache2
$ sudo systemctl restart apache2
$

잘 적용되었는지 확인해보자

$ ls -l /etc/apache2/mods-enabled | grep rewrite
lrwxrwxrwx 1 root root 30 Feb 24 12:35 rewrite.load -> ../mods-available/rewrite.load
$

mods-enable에 링크로 rewrite.load 파일이 설정되어 있고, apache2 재시작 시에 오류가 나지 않았다면... 잘 적용되었을 것이다. 더 확실하게 확인하고 싶다면..

$ cd /var/www/html
$ cat info.php
<?php phpinfo(); ?>
$

PHP 정보를 확인할 수 있는 info.php를 만든 뒤에 해당 화면을 호출해보자.

직접 캡쳐

해당 화면에서 mod_rewrite가 검색된다면 잘 적용된 것으로 확신해도 될 것 같다.

D. site 설정

simplism.local이라는 도메인을 예를 들어서 설명해보려고 한다. wiki.simplism.local을 호출하면 /var/www/html/dokuwiki를 찾아가고, simplism.local은 /var/www/html을 찾아가게 설정하려고 한다.

$ cd /etc/apache2/sites-enabled/
$ cat dokuwiki.conf
<VirtualHost *:80>
        ServerName    wiki.simplism.local
        DocumentRoot  /var/www/html/dokuwiki

        <Directory ~ "/var/www/html/dokuwiki/(bin/|conf/|data/|inc/)">
            <IfModule mod_authz_core.c>
                AllowOverride All
                Require all denied
            </IfModule>
            <IfModule !mod_authz_core.c>
                Order allow,deny
                Deny from all
            </IfModule>
        </Directory>

        ErrorLog   /var/log/apache2/dokuwiki_error.log
        CustomLog  /var/log/apache2/dokuwiki_access.log combined
</VirtualHost>
$

위가 wiki를 설치할 때 기본으로 설정했던 site 설정파일이다.

        <Directory "/var/www/html/dokuwiki">
                AllowOverride All
        </Directory>

rewrite 설정이 동작하게 하려면, /var/www/html/dokuwiki 경로에 대해서 AllowOverride 설정이 되어있어야 한다. 그렇게 하기 위해서는 위 내용을 추가한다.

최종 설정파일의 상태는 아래와 같다.

$ cat dokuwiki.conf
<VirtualHost *:80>
        ServerName    wiki.simplism.local
        DocumentRoot  /var/www/html/dokuwiki

        <Directory "/var/www/html/dokuwiki">
                AllowOverride All
        </Directory>
        <Directory ~ "/var/www/html/dokuwiki/(bin/|conf/|data/|inc/)">
            <IfModule mod_authz_core.c>
                AllowOverride All
                Require all denied
            </IfModule>
            <IfModule !mod_authz_core.c>
                Order allow,deny
                Deny from all
            </IfModule>
        </Directory>

        ErrorLog   /var/log/apache2/dokuwiki_error.log
        CustomLog  /var/log/apache2/dokuwiki_access.log combined
</VirtualHost>
$

E. .htaccess 설정

dokuwiki 설치경로에 있는 .htaccess 파일을 수정해줘야한다.

$ cd /var/www/html/dokuwiki
$ ls -al | grep .htaccess
-rw-r--r--  1 root     root      1668 Feb 24 11:07 .htaccess
-rw-r--r--  1     2012     2012  1688 Feb 12 21:09 .htaccess.dist
$ cat .htaccess
## You should disable Indexes and MultiViews either here or in the
## global config. Symlinks maybe needed for URL rewriting.
#Options -Indexes -MultiViews +FollowSymLinks

## make sure nobody gets the htaccess, README, COPYING or VERSION files
<Files ~ "^([\._]ht|README$|VERSION$|COPYING$)">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</Files>

## Don't allow access to git directories
<IfModule alias_module>
    RedirectMatch 404 /\.git
</IfModule>

## Uncomment these rules if you want to have nice URLs using
## $conf['userewrite'] = 1 - not needed for rewrite mode 2
#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
#
## Not all installations will require the following line.  If you do,
## change "/dokuwiki" to the path to your dokuwiki directory relative
## to your document root.
#RewriteBase /dokuwiki
#
## If you enable DokuWikis XML-RPC interface, you should consider to
## restrict access to it over HTTPS only! Uncomment the following two
## rules if your server setup allows HTTPS.
#RewriteCond %{HTTPS} !=on
#RewriteRule ^lib/exe/xmlrpc.php$      https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
$
#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
#
## Not all installations will require the following line.  If you do,
## change "/dokuwiki" to the path to your dokuwiki directory relative
## to your document root.
#RewriteBase /dokuwiki

위 부분은 기본적으로 설정파일에 '#' 문자로 주석처리가 되어있는데, '#' 문자를 지워서 설정을 켜준다.

추가적으로 아래부분도 고쳐준다.

RewriteBase /dokuwiki

이렇게

RewriteBase /

이렇게해서.. 최종적인 .htaccess 파일의 내용은 아래와 같다.

$ cat .htaccess
## You should disable Indexes and MultiViews either here or in the
## global config. Symlinks maybe needed for URL rewriting.
Options -Indexes -MultiViews +FollowSymLinks

## make sure nobody gets the htaccess, README, COPYING or VERSION files
<Files ~ "^([\._]ht|README$|VERSION$|COPYING$)">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</Files>

## Don't allow access to git directories
<IfModule alias_module>
    RedirectMatch 404 /\.git
</IfModule>

## Uncomment these rules if you want to have nice URLs using
## $conf['userewrite'] = 1 - not needed for rewrite mode 2
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
#
## Not all installations will require the following line.  If you do,
## change "/dokuwiki" to the path to your dokuwiki directory relative
## to your document root.
RewriteBase /
#
## If you enable DokuWikis XML-RPC interface, you should consider to
## restrict access to it over HTTPS only! Uncomment the following two
## rules if your server setup allows HTTPS.
#RewriteCond %{HTTPS} !=on
#RewriteRule ^lib/exe/xmlrpc.php$      https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
$

.htaccess의 원본과 서로 비교하면 아래와 같다.

$ diff .htaccess.dist .htaccess
3c3
< #Options -Indexes -MultiViews +FollowSymLinks
---
> Options -Indexes -MultiViews +FollowSymLinks
23,32c23,32
< #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
---
> 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
37c37
< #RewriteBase /dokuwiki
---
> RewriteBase /
$

참고) RewriteBase

특히 RewriteBase의 설정부분이 중요한데..

지금까지 설정을 wiki.simplism.local -> /var/www/html/dokuwiki로 VirtualHost 설정을 했기 때문에 RewriteBase를 '/'로 설정해야한다.

http://simplism.local/dokuwiki

만약에 위와 같이 dokuwiki를 사용하기 위해서 아래와 같이 VirtualHost 설정을 했다면..?

ServerName simplism.local
DocumentRoot /var/www/html

그 때는 RewriteBase의 값은 '/dokuwiki'로 설정해야한다.

F. dokuwiki 설정변경

직접 캡쳐

이제 서버측 설정은 끝났으니 dokuwiki 환경 설정 관리자에서 멋진 URL 사용.htaccess로 변경해준다.

그리고 rewrite된 URL을 호출해보자!

직접 캡쳐

위와 같이 정상적으로 dokuwiki 페이지가 열리면 완료!

G. 마치면서..

나중에 다시봐도 확실하게 설정값을 볼 수 있도록 전체 설정파일을 cat 명령어로 확인한 것을 다 넣다보니... 내용에 비해서 포스트가 길다.

뭔가 더 효과적이면서 축약적으로 내용을 기록할 수는 없을까? (너무 긴 설정파일은 첨부파일로??)

글쓰기는 어렵다.