Mediawiki实现短链接伪静态

来自维基鲸
这篇文章需要改进。你可以帮助维基鲸来 编辑它
Xiaotian留言) 2024年4月09日 (四) 20:17 (CST)
现在的配置,在访问Common.js、图片文件等,会提示报错404


一、Apache和Nginx配置

1.程序位于根目录/

Apache: .htaccess 文件

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ /index.php?title=$1
RewriteRule ^/*$ /index.php
RewriteRule ^(.*)$ /index.php

Nginx:

location / {
  if (!-e $request_filename){
    rewrite ^(.*)$ /index.php?title=$1;
  }
  rewrite ^/*$ /index.php;
  rewrite ^(.*)$ /index.php;
}

2.程序位于根目录/wiki/

Apache: .htaccess 文件

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wiki/(.*)$ wiki/index.php?title=$1
RewriteRule ^wiki/*$ wiki/index.php
RewriteRule ^wiki$ wiki/index.php

Nginx:

location / {
  if (!-e $request_filename){
    rewrite ^/wiki/(.*)$ /wiki/index.php?title=$1;
  }
}

location /wiki {
  rewrite ^/wiki/*$ /wiki/index.php;
}

location = /wiki {
  rewrite ^(.*)$ /wiki/index.php;
}

二、LocalSettings.php配置

打开 Wiki 程序下的 LocalSettings.php ,在末尾加上如下代码。

$wgArticlePath = "/wiki/$1";
$wgUsePathInfo = true;

如果需要修改程序其他动作,可添加如下代码。

$wgArticlePath = "/wiki/$1";
$wgUsePathInfo = true;

$actions = ['edit', 'watch', 'unwatch', 'delete','revert', 'rollback', 'protect', 'unprotect', 'markpatrolled', 'render', 'submit', 'history', 'purge', 'info'];
foreach($actions as $action) {
    $wgActionPaths[$action] = "/wiki/$action/$1";
}
$wgActionPaths["view"] =  "/wiki/$1";

三、新样式

原始样式:http://wiki.xtboke.com/index.php?title=首页

短链接:http://wiki.xtboke.com/wiki/首页

更新:2024-04-11

程序安装在根目录,伪静态更新为如下之后,再访问Common.js、图片文件等,404报错消失。

location ~ ^\/.+$ {
if ($request_uri ~ ^/images) { break; }
if ($request_uri ~ ^/resources) { break; }
if ($request_uri ~ ^/index\.php) { break; }
rewrite ^/(.+)$ /index.php?title=$1 last;
}
location /rest.php/ {
try_files $uri $uri/ /rest.php? $query_string ;
}