配置MW:短链接
		
		
		
		
		
		跳到导航
		跳到搜索
		关于:
		
	
关于
短链接,顾名思义,相较于一长串字母的、毫无意义的、复杂的网址,短链接(如:“http://wiki.eijux.com/首页”)更易于理解、记忆、管理。 短 URL 或 URL 重写会隐藏页面地址中的 php 文件扩展名。
关于:相关 LocalSettings.php 配置项
相关配置项:
- $wgScriptPath:MediaWiki 文件的实际路径。
- $wgArticlePath:MediaWiki 文章的虚拟路径。
- 必须与“$wgScriptPath”不同。
 
- $wgUsePathInfo:为操作获取特殊链接。
- $wgScriptExtension:脚本扩展名。
- $wgActionPaths:MediaWiki 文章的操作路径。
关于:相关 Nginx 指令
相关指令:
- try_files:尝试读取文件(读取静态文件),按后面的配置一次尝试。
- $uri:Nginx 的一个变量,存放着用户访问的地址。
- 如:“http://www.xxx.com/index.html”,那么“$uri”就是“/index.html”;
 
- 如:“
- $uri/:Nginx 的一个变量,代表访问的是一个目录。
- 如:“http://www.xxx.com/hello/test/”,那么“$uri/”就是“/hello/test/”;
 
- 如:“
示例:try_files $uri $uri/ @rewrite;
说明:
- 尝试到网站目录读取用户访问的文件,如果“$uri”存在,就直接返回;
- 否则继续读取“$uri/”,如果存在直接返回;
- 否则直接跳转到“@rewrite”(定义的另一个“Location”)。
 
- 尝试到网站目录读取用户访问的文件,如果“
关于:http://wiki.eijux.com/%E9%A6%96%E9%A1%B5
地址“http://wiki.eijux.com/%E9%A6%96%E9%A1%B5”与“http://wiki.eijux.com/首页”其实是同一个地址:前者是将地址中的中文通过“URL 编码[1]”得到的。
内容短链接[2]:$wgArticlePath
默认“内容地址”:http://wiki.eijux.com/index.php?title=首页。
MediaWiki 的短链接有两种方案[3]:
- example.com/wiki/Page_title。
- example.com/Page_title。
关于“example.com/Page_title”方案:1、使用域的根目录作为 wiki 目录[4];2、使用 URL 如“example.com/Page_title”。
但这并不是官方建议的方式:
1、仅使用于“域名(子域名)仅用于 MediaWiki”。  ——  否则,可能会导致与其他文件和目录冲突。
2、可能存在的 Bug,如:task T34621、task T40048。等。
example.com/wiki/Page_title[5]
1、MediaWiki 安装在站点目录下的文件夹中(“/www/wwwroot/example.com/w/”)。 2、文章将以“example.com/wiki/Page_title”形式的 URL 显示。
配置:
- LocalSettings.php:
- $wgScriptPath = "/w"; $wgArticlePath = "/wiki/$1"; $wgUsePathInfo = true; 
 
- example.com.conf[6][7]:
- server { # [...] # Location for wiki's entry points location ~ ^/w/(index|load|api|thumb|opensearch_desc|rest|img_auth)\.php$ { include /etc/nginx/fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; # or whatever port your PHP-FPM listens on } # Images location /w/images { # Separate location for images/ so .php execution won't apply } location /w/images/deleted { # Deny access to deleted images folder deny all; } # MediaWiki assets (usually images) location ~ ^/w/resources/(assets|lib|src) { try_files $uri 404; add_header Cache-Control "public"; expires 7d; } # Assets, scripts and styles from skins and extensions location ~ ^/w/(skins|extensions)/.+\.(css|js|gif|jpg|jpeg|png|svg|wasm)$ { try_files $uri 404; add_header Cache-Control "public"; expires 7d; } # Favicon location = /favicon.ico { alias /w/images/6/64/Favicon.ico; add_header Cache-Control "public"; expires 7d; } # License and credits files location ~ ^/w/(COPYING|CREDITS)$ { default_type text/plain; } ## Uncomment the following code if you wish to use the installer/updater ## installer/updater #location /w/mw-config/ { # # Do this inside of a location so it can be negated # location ~ \.php$ { # include /etc/nginx/fastcgi_params; # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # fastcgi_pass 127.0.0.1:9000; # or whatever port your PHP-FPM listens on # } #} # Handling for Mediawiki REST API, see [[mw:API:REST_API]] location /w/rest.php/ { try_files $uri $uri/ /w/rest.php?$query_string; } ## Uncomment the following code for handling image authentication ## Also add "deny all;" in the location for /w/images above #location /w/img_auth.php/ { # try_files $uri $uri/ /w/img_auth.php?$query_string; #} # Handling for the article path (pretty URLs) location /wiki/ { rewrite ^/wiki/(?<pagename>.*)$ /w/index.php; } # Allow robots.txt in case you have one location = /robots.txt { } # Explicit access to the root website, redirect to main page (adapt as needed) location = / { return 301 /wiki/Main_Page; } # Every other entry point will be disallowed. # Add specific rules for other entry points/images as needed above this location / { return 404; } }
 
example.com/Page_title[8]
1、MediaWiki 安装在站点目录的根目录中(“/www/wwwroot/example.com/”)。 2、文章将以“example.com/Page_title”形式的 URL 显示。
配置:
- LocalSettings.php:
- $wgScriptPath = ""; $wgArticlePath = "/$1"; $wgUsePathInfo = true; $wgScriptExtension = ".php"; 
 
- example.com.conf[6][7]:
- server { server_name www.example.com example.com; listen 80; root /home/user/public_html; index index.php index.html index.htm; access_log /var/log/nginx/access-example.log; error_log /var/log/nginx/error-example.log; location ~ \.ht { deny all; } location / { try_files $uri $uri/ @rewrite; } location @rewrite { rewrite ^/(.*)$ /index.php; } location ^~ /maintenance/ { return 403; } location ~ \.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; try_files $uri @rewrite; } }
 
操作短链接:$wgActionPaths
设置以上$wgArticlePath等设置之后,页面的链接变为了短链接(如:http://wiki.eijux.com/首页),但是页面的编辑、移动、删除、历史等页面,仍然是长链接页面(如:http://wiki.eijux.com/首页?action=edit&veswitched=1),若要使操作页面也应用短链接,则应该设置$wgActionPaths。(参见Manual:$wgActionPaths)
如,设置Action在页面之后:
#  Short URL of Action
$actions = array( 'edit', 'watch', 'unwatch', 'delete','revert', 'rollback', 'protect', 'unprotect', 'markpatrolled', 'render', 'submit', 'history', 'purge', 'info' );
foreach ( $actions as $action ) {
	$wgActionPaths[$action] = "/$1/$action";
}
$wgActionPaths['view'] = "/$1";
$wgArticlePath = $wgActionPaths['view'];
则页面编辑链接为http://wiki.eijux.com/首页/edit。
- 但是:
- 使用$wgActionPaths设置,则不能创建某些与$actions中定义的操作词相关的页面,如:创建名为“首页/edit”(action后置)、“edit/首页”(action后置)、“首页/history”的页面时,会跳转到“首页”相关的操作页面。所以不建议使用,或者修改$actions的操作词为不常用词(可能需要修改页面代码?)。
 
- 使用
备注
当前站点使用的方式为:#example.com/Page_title。现备注正在使用的“wiki.eijux.com.conf”如下。
【2022/08/23 05:41:49】:
- server { listen 80; listen 443 ssl http2; server_name wiki.eijux.com; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/wiki.eijux.com; #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则 #error_page 404/404.html; ssl_certificate /www/server/panel/vhost/cert/wiki.eijux.com/fullchain.pem; ssl_certificate_key /www/server/panel/vhost/cert/wiki.eijux.com/privkey.pem; ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; add_header Strict-Transport-Security "max-age=31536000"; error_page 497 https://$host$request_uri; #SSL-END #ERROR-PAGE-START 错误页配置,可以注释、删除或修改 #error_page 404 /404.html; #error_page 502 /502.html; #ERROR-PAGE-END #PHP-INFO-START PHP引用配置,可以注释或修改 include enable-php-74.conf; #PHP-INFO-END #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效 include /www/server/panel/vhost/rewrite/wiki.eijux.com.conf; #REWRITE-END #禁止访问的文件或目录 location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) { return 404; } #一键申请SSL证书验证目录相关设置 location ~ ^/.well-known/{ allow all; } #禁止在证书验证目录放入敏感文件 if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) { return 403; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; error_log /dev/null; access_log /dev/null; #允许访问内容, by Eijux at 2022/08/21 23:33:42 try_files $uri @rewrite; } location ~ .*\.(js|css)?$ { expires 12h; error_log /dev/null; access_log /dev/null; #允许访问内容, by Eijux at 2022/08/21 23:33:42 try_files $uri @rewrite; } #修改短链接, by Eijux at 2020/09/09 03:14:50 #begin location ~ \.ht { deny all; } location / { try_files $uri $uri/ @rewrite; } location @rewrite { rewrite ^/(.*)$ /index.php; } location ^~ /maintenance/ { return 403; } location ~ \.php$ { include /www/server/nginx/conf/fastcgi_params; fastcgi_pass unix:/tmp/php-cgi-73.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; try_files $uri @rewrite; } #end access_log /www/wwwlogs/wiki.eijux.com.log; error_log /www/wwwlogs/wiki.eijux.com.error.log; } 
LocalSettings.php设置
如下:
# Short URL
$wgArticlePath = "/$1";
$wgUsePathInfo = true;
$wgScriptExtension = ".php";
nginx configuration设置
修改站点的nginx配置文件,使用BTPabel安装的LNMP环境,可在面板操作“网站->wiki.eijux.com设置->配置文件”进行设置。
(宝塔中站点的nginx配置文件(如"wiki.eijux.com.conf"),位于"/www/server/panel/vhost/nginx")
添加:
    location / {
        try_files $uri $uri/ @rewrite;
    }
    location @rewrite {
        rewrite ^/(.*)$ /index.php;
    }
就可完成短链接的转换。另:
    # 限制访问.ht文件
    location ~ \.ht {
        deny all;
    }
    # 限制对maintenance该路径的访问,返回403
    location ^~ /maintenance/ {
        return 403;
    }
    # 以下配置允许运行.php的程序
    # 关于fastcgi的配置,参见"/www/server/nginx/conf"中的"enable-php-73.conf"
    # ("fastcgi_params"和"fastcgi.conf"均位于"/www/server/nginx/conf"中)
    location ~ \.php$ {
        include /www/server/nginx/conf/fastcgi_params;
        fastcgi_pass  unix:/tmp/php-cgi-73.sock;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME	$document_root$fastcgi_script_name;
        try_files $uri @rewrite;
    }
FAQ
修改短链接之后,图片页404
以上,修改站点短链接之后,网页、特殊页面均无问题,只有png、jpg等图片页访问404(图片列表、svg页无问题)。
即:站点的nginx配置文件中配置的类型,无法访问:
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        ...
    }
    
    location ~ .*\.(js|css)?$
    {
        ...
    }
需要在Location中配置try_files。
修改配置
如下:
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log /dev/null;
        try_files $uri @rewrite;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log /dev/null; 
        try_files $uri @rewrite;
    }
备注: 【2020/09/15 00:40:12】Done.
参考
- ↑ URL 编码,工具站点:https://www.bejson.com/enc/urlencode/
- ↑ 参考:Manual:Short_URL
- ↑ 配置短链接之后,长链接(http://wiki.eijux.com/index.php?title=首页)、短链接(http://wiki.eijux.com/首页)均可正常访问。
- ↑ Manual:Wiki in site root directory
- ↑ 参考:Manual:Short_URL/Nginx
- ↑ 6.0 6.1 站点的 Nginx 配置:
 1、可以通过“宝塔面板”修改(网站 -> 站点设置 -> 配置文件);
 2、也可以通过服务器上文件修改(位置:“/www/server/panel/vhost/nginx/example.com.conf”)。
- ↑ 7.0 7.1 修改 Nginx 配置之后,可能需要重载 Nginx 配置:
 1、在宝塔面板的“Nginx 管理”中“重载配置”;
 2、通过命令。
- ↑ 参考:Manual:Short URL/Page title - nginx, Root Access, PHP as a CGI module