“配置MW:短链接”的版本间差异
跳到导航
跳到搜索
关于:
(未显示同一用户的20个中间版本) | |||
第1行: | 第1行: | ||
[[MediaWiki]] | [[category:MediaWiki]] | ||
== 关于 == | |||
短链接,顾名思义,相较于一长串字母的、毫无意义的、复杂的网址,短链接(如:“[http://wiki.eijux.com/首页 http://wiki.eijux.com/首页]”)更易于理解、记忆、管理<ref>[https://www.w3.org/Provider/Style/URI Cool URIs don't change]</ref>。 | |||
短 URL 或 URL 重写会隐藏页面地址中的 php 文件扩展名。 | |||
== | === 关于:相关 LocalSettings.php 配置项 === | ||
''' | 相关配置项: | ||
# <span style="color: green">'''$wgScriptPath'''</span>:MediaWiki 文件的实际路径。 | |||
# <span style="color: green">'''$wgArticlePath'''</span>:MediaWiki 文章的虚拟路径。 | |||
#* 必须与“$wgScriptPath”不同。 | |||
# <span style="color: green">$wgUsePathInfo</span>:为操作获取特殊链接。 | |||
# <span style="color: green">$wgScriptExtension</span>:脚本扩展名。 | |||
# <span style="color: green">'''$wgActionPaths'''</span>:MediaWiki 文章的操作路径。 | |||
=== LocalSettings. | === 关于:相关 Nginx 指令 === | ||
相关指令: | |||
< | # <span style="color: green">'''try_files'''</span>:尝试读取文件(读取静态文件),按后面的配置一次尝试。 | ||
# <span style="color: green">'''$uri'''</span>:Nginx 的一个变量,存放着用户访问的地址。 | |||
#: 如:“<code>http://www.xxx.com/index.html</code>”,那么“$uri”就是“<code>/index.html</code>”; | |||
# <span style="color: green">'''$uri/'''</span>:Nginx 的一个变量,代表访问的是一个目录。 | |||
#: 如:“<code>http://www.xxx.com/hello/test/</code>”,那么“$uri/”就是“<code>/hello/test/</code>”; | |||
: '''示例:''' '''<syntaxhighlight lang="PHP" inline>try_files $uri $uri/ @rewrite;</syntaxhighlight>''' | |||
: '''说明:''' | |||
::# 尝试到网站目录读取用户访问的文件,如果“<code>$uri</code>”存在,就直接返回; | |||
::# 否则继续读取“<code>$uri/</code>”,如果存在直接返回; | |||
::# 否则直接跳转到“<code>@rewrite</code>”(定义的另一个“Location”)。 | |||
=== 关于:<code>http://wiki.eijux.com/%E9%A6%96%E9%A1%B5</code> === | |||
地址“[http://wiki.eijux.com/%E9%A6%96%E9%A1%B5 http://wiki.eijux.com/%E9%A6%96%E9%A1%B5]”与“[http://wiki.eijux.com/首页 http://wiki.eijux.com/首页]”其实是同一个地址:前者是将地址中的<span style="color: green">中文</span>通过“'''URL 编码'''<ref>URL 编码,工具站点:[https://www.bejson.com/enc/urlencode/ https://www.bejson.com/enc/urlencode/]</ref>”得到的。 | |||
== 内容短链接<ref>参考:'''[https://www.mediawiki.org/wiki/Manual:Short_URL Manual:Short_URL]'''</ref> == | |||
默认“内容地址”:<code>[http://wiki.eijux.com/index.php?title=首页 http://wiki.eijux.com/index.php?title=首页]</code>。 | |||
MediaWiki 的短链接有两种方案<ref>配置短链接之后,长链接([http://wiki.eijux.com/index.php?title=首页 http://wiki.eijux.com/index.php?title=首页])、短链接([http://wiki.eijux.com/首页 http://wiki.eijux.com/首页])均可正常访问。</ref>: | |||
# <span style="color: blue; font-size: 120%">'''example.com/wiki/Page_title'''</span> | |||
#: 如:[http://wiki.eijux.com/wiki/首页 http://wiki.eijux.com/wiki/首页] | |||
# <span style="color: blue; font-size: 120%">'''example.com/Page_title'''</span> | |||
#: 如:[http://wiki.eijux.com/首页 http://wiki.eijux.com/首页] | |||
关于“<span style="color: green">'''example.com/Page_title'''</span>”方案:1、'''使用域的根目录作为 wiki 目录'''<ref>[https://www.mediawiki.org/wiki/Manual:Wiki_in_site_root_directory Manual:Wiki in site root directory]</ref>;2、使用 URL 如“<code>example.com/Page_title</code>”。 | |||
但这并不是官方建议的方式: | |||
1、<span style="color: green">仅适用于“'''域名(子域名)仅用于 MediaWiki'''”</span>。 —— 否则,可能会导致与其他文件和目录冲突。 | |||
2、可能存在的 Bug,如:[https://phabricator.wikimedia.org/T34621 task T34621]、[https://phabricator.wikimedia.org/T40048 task T40048]。'''等。 | |||
=== example.com/wiki/Page_title<ref>参考:'''[https://www.mediawiki.org/wiki/Manual:Short_URL/Nginx Manual:Short_URL/Nginx]'''</ref> === | |||
---- | |||
1、MediaWiki 安装在站点目录下的文件夹中(“/www/wwwroot/example.com/'''w'''/”)。 | |||
2、文章将以“example.com/'''wiki'''/Page_title”形式的 URL 显示。 | |||
配置: | |||
# '''LocalSettings.php''': | |||
#: <syntaxhighlight lang="PHP" line highlight="1-3"> | |||
$wgScriptPath = "/w"; | |||
$wgArticlePath = "/wiki/$1"; | |||
$wgUsePathInfo = true; | |||
</syntaxhighlight> | |||
# '''example.com.conf'''<ref name="Nginx_config_of_site">站点的 Nginx 配置:<br/>1、可以通过“<span style="color: green">'''宝塔面板'''</span>”修改(<span style="color: green">'''网站 -> 站点设置 -> 配置文件'''</span>);<br/>2、也可以通过服务器上“<span style="color: green">'''配置文件'''</span>”修改(位置:“<span style="color: green">'''/www/server/panel/vhost/nginx/example.com.conf'''</span>”)。</ref><ref name="Nginx_reload">修改 Nginx 配置之后,可能需要'''重载 Nginx 配置''':<br/>1、在宝塔面板的“Nginx 管理”中“重载配置”;<br/>2、通过命令。</ref>: | |||
#: <syntaxhighlight lang="PHP" line highlight="4-76"> | |||
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; | |||
} | |||
} | |||
</syntaxhighlight> | |||
=== '''example.com/Page_title'''<ref>参考:'''[https://www.mediawiki.org/wiki/Manual:Short_URL/Page_title_-_nginx,_Root_Access,_PHP_as_a_CGI_module Manual:Short URL/Page title - nginx, Root Access, PHP as a CGI module]'''</ref> === | |||
---- | |||
1、MediaWiki 安装在站点目录的根目录中(“/www/wwwroot/example.com/”)。 | |||
2、文章将以“example.com/Page_title”形式的 URL 显示。 | |||
配置: | |||
# '''LocalSettings.php''': | |||
#: <syntaxhighlight lang="PHP" line highlight="1-4"> | |||
$wgScriptPath = ""; | |||
$wgArticlePath = "/$1"; | |||
$wgUsePathInfo = true; | |||
$wgScriptExtension = ".php"; | |||
</syntaxhighlight> | |||
# '''example.com.conf'''<ref name="Nginx_config_of_site"/><ref name="Nginx_reload"/>: | |||
#: <syntaxhighlight lang="PHP" line highlight="15-21"> | |||
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; | |||
} | |||
} | |||
</syntaxhighlight> | |||
== 操作<ref>“操作”包括:页面的“编辑、移动、删除、保护、历史”等等</ref>短链接<ref>参考:'''[https://www.mediawiki.org/wiki/Manual:$wgActionPaths Manual:$wgActionPaths]'''</ref> == | |||
必须先设置'''[[#内容短链接]]''',并保证有效。 | |||
默认“操作地址”:<code>[http://wiki.eijux.com/index.php?title=首页&action=edit http://wiki.eijux.com/index.php?title=首页&action=edit]</code>。 | |||
MediaWiki 的“操作短链接”有多种方案: | |||
# <span style="color: blue">'''根路径'''</span>: | |||
#: <syntaxhighlight lang="PHP" line highlight=""> | |||
$actions = array( 'view', 'edit', 'watch', 'unwatch', 'delete','revert', 'rollback', | |||
'protect', 'unprotect', 'markpatrolled', 'render', 'submit', 'history', 'purge', 'info' ); | |||
foreach ( $actions as $action ) { | |||
#操作前置: | |||
$wgActionPaths[$action] = "/$action/$1"; | |||
#操作后置: | |||
$wgActionPaths[$action] = "/$1/$action"; | |||
} | |||
$wgActionPaths['view'] = "/$1"; | |||
$wgArticlePath = $wgActionPaths['view']; | |||
</syntaxhighlight> | |||
#: 操作前置:<code>http://wiki.eijux.com'''/edit/首页'''</code>”; | |||
#: 操作后置:<code>http://wiki.eijux.com'''/首页/edit'''</code>”; | |||
# <span style="color: blue">'''非根路径'''</span>: | |||
#: <syntaxhighlight lang="PHP" line highlight=""> | |||
$actions = array( 'edit', 'watch', 'unwatch', 'delete','revert', 'rollback', | |||
'protect', 'unprotect', 'markpatrolled', 'render', 'submit', 'history', 'purge', 'info' ); | |||
foreach ( $actions as $action ) { | |||
#操作前置: | |||
$wgActionPaths[$action] = "/wiki/$action/$1"; | |||
#操作后置: | |||
$wgActionPaths[$action] = "/wiki/$1/$action"; | |||
} | |||
$wgActionPaths['view'] = "/wiki/$1"; | |||
$wgArticlePath = $wgActionPaths['view']; | |||
</syntaxhighlight> | |||
#: 操作前置:“<code>http://wiki.eijux.com'''/wiki/edit/首页'''</code>”; | |||
#: 操作后置:“<code>http://wiki.eijux.com'''/wiki/首页/edit'''</code>”; | |||
# <span style="color: blue">'''虚拟路径'''</span>: | |||
#: <syntaxhighlight lang="PHP" line highlight=""> | |||
$actions = array( 'view', 'edit', 'watch', 'unwatch', 'delete','revert', 'rollback', | |||
'protect', 'unprotect', 'markpatrolled', 'render', 'submit', 'history', 'purge', 'info' ); | |||
foreach ( $actions as $action ) { | |||
#操作前置: | |||
$wgActionPaths[$action] = "$wgScriptPath/action/$action/$1"; | |||
#操作后置: | |||
$wgActionPaths[$action] = "$wgScriptPath/action/$1/$action"; | |||
} | |||
$wgActionPaths['view'] = $wgScriptPath/action/$1"; | |||
$wgArticlePath = $wgActionPaths['view']; | |||
</syntaxhighlight> | |||
#: 操作前置:“<code>http://wiki.eijux.com'''/wiki/action/edit/首页'''</code>”; | |||
#: 操作后置:“<code>http://wiki.eijux.com'''/wiki/action/首页/edit'''</code>”; | |||
* 以上均可分为“<span style="color: blue">'''操作前置'''</span>”和“<span style="color: blue">'''操作后置'''</span>”。 | |||
* 以上均设置单独将 view 操作链接中的“view”关键字省略。 | |||
<span style="color: red">'''配置了以上内容,仍然需要在“站点的 Ngnix 配置文件中”(example.com.conf)中定义规则以完成 URL 重写。'''</span> | |||
但是,使用“<span style="color: green">'''$wgActionPaths'''</span>”设置后,不能创建某些与“操作关键字”(如上“<code>'''$actions'''</code>”中定义)相关的页面。 | |||
如:创建名为“首页/edit”(action后置)、“edit/首页”(action后置)、“首页/history”的页面时,会跳转到“首页”相关的操作页面。 | |||
对此,可以修改“<span style="color: green">'''$actions'''</span>”定义的操作词为不常用词(可能需要修改页面代码?)。 | |||
== 备注 == | |||
当前站点使用的方式为:<span style="color: blue">'''example.com/Page_title'''</span>,现备注正在使用的“wiki.eijux.com.conf”如下。 | |||
【2022/08/23 05:41:49】 | |||
# LocalSettings.php: | |||
#: <syntaxhighlight lang="PHP" line highlight=""> | |||
# Short URL | # Short URL | ||
$wgArticlePath = "/$1"; | $wgArticlePath = "/$1"; | ||
$wgUsePathInfo = true; | $wgUsePathInfo = true; | ||
$wgScriptExtension = ".php"; | $wgScriptExtension = ".php"; | ||
</ | </syntaxhighlight> | ||
# wiki.eijux.com.conf:'''【关键部分已高亮】''' | |||
#: <syntaxhighlight lang="PHP" line highlight="11-12,21-22,31-37"> | |||
server | |||
{ | |||
... | |||
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 / { | location / { | ||
try_files $uri $uri/ @rewrite; | try_files $uri $uri/ @rewrite; | ||
第30行: | 第319行: | ||
rewrite ^/(.*)$ /index.php; | rewrite ^/(.*)$ /index.php; | ||
} | } | ||
location ^~ /maintenance/ { | location ^~ /maintenance/ { | ||
return 403; | return 403; | ||
} | } | ||
location ~ \.php$ { | location ~ \.php$ { | ||
include /www/server/nginx/conf/fastcgi_params; | include /www/server/nginx/conf/fastcgi_params; | ||
fastcgi_pass unix:/tmp/php-cgi-73.sock; | fastcgi_pass unix:/tmp/php-cgi-73.sock; | ||
fastcgi_index index.php; | fastcgi_index index.php; | ||
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | ||
try_files $uri @rewrite; | try_files $uri @rewrite; | ||
} | } | ||
#end | |||
access_log /www/wwwlogs/wiki.eijux.com.log; | |||
error_log /www/wwwlogs/wiki.eijux.com.error.log; | |||
} | } | ||
</syntaxhighlight> | |||
#* 关于fastcgi的配置,参见“/www/server/nginx/conf”中的“enable-php-73.conf”。 | |||
</ | #**“fastcgi_params”和“fastcgi.conf”均位于“/www/server/nginx/conf”中。 | ||
== FAQ == | |||
=== '''404 Not Found nginx''' === | |||
在修改短链接之后,可能出现 <span style="color: red; font-size: 140%">'''404 Not Found nginx'''</span> 的错误: | |||
[[File:Nginx:404.png|800px]] | |||
= | 出现此错误,其实是<span style="color: blue">'''“站点的 Ngnix 配置文件”(example.com.conf)配置有问题'''</span>: | ||
# 若在访问'''所有页面'''均出现此错误,则参考 <span style="font-size: 120%">'''[[#内容短链接]]'''</span> 进行修改即可。 | |||
# 若仅访问'''“png、jpg”、“js、css”等内容'''时,出现此错误,则修改如下: | |||
#: <syntaxhighlight lang="PHP" line highlight="7,16"> | |||
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; | |||
} | |||
</syntaxhighlight> | |||
若有其他内容错误,解决类似。 | |||
== 参考 == | |||
<references/> |
2022年8月23日 (二) 22:49的最新版本
关于
短链接,顾名思义,相较于一长串字母的、毫无意义的、复杂的网址,短链接(如:“http://wiki.eijux.com/首页”)更易于理解、记忆、管理[1]。 短 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 编码[2]”得到的。
内容短链接[3]
默认“内容地址”:http://wiki.eijux.com/index.php?title=首页
。
MediaWiki 的短链接有两种方案[4]:
- example.com/wiki/Page_title
- example.com/Page_title
关于“example.com/Page_title”方案:1、使用域的根目录作为 wiki 目录[5];2、使用 URL 如“example.com/Page_title
”。
但这并不是官方建议的方式:
1、仅适用于“域名(子域名)仅用于 MediaWiki”。 —— 否则,可能会导致与其他文件和目录冲突。
2、可能存在的 Bug,如:task T34621、task T40048。等。
example.com/wiki/Page_title[6]
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[7][8]:
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[9]
1、MediaWiki 安装在站点目录的根目录中(“/www/wwwroot/example.com/”)。 2、文章将以“example.com/Page_title”形式的 URL 显示。
配置:
- LocalSettings.php:
$wgScriptPath = ""; $wgArticlePath = "/$1"; $wgUsePathInfo = true; $wgScriptExtension = ".php";
- example.com.conf[7][8]:
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; } }
操作[10]短链接[11]
必须先设置#内容短链接,并保证有效。
默认“操作地址”:http://wiki.eijux.com/index.php?title=首页&action=edit
。
MediaWiki 的“操作短链接”有多种方案:
- 根路径:
$actions = array( 'view', 'edit', 'watch', 'unwatch', 'delete','revert', 'rollback', 'protect', 'unprotect', 'markpatrolled', 'render', 'submit', 'history', 'purge', 'info' ); foreach ( $actions as $action ) { #操作前置: $wgActionPaths[$action] = "/$action/$1"; #操作后置: $wgActionPaths[$action] = "/$1/$action"; } $wgActionPaths['view'] = "/$1"; $wgArticlePath = $wgActionPaths['view'];
- 操作前置:
http://wiki.eijux.com/edit/首页
”; - 操作后置:
http://wiki.eijux.com/首页/edit
”;
- 非根路径:
$actions = array( 'edit', 'watch', 'unwatch', 'delete','revert', 'rollback', 'protect', 'unprotect', 'markpatrolled', 'render', 'submit', 'history', 'purge', 'info' ); foreach ( $actions as $action ) { #操作前置: $wgActionPaths[$action] = "/wiki/$action/$1"; #操作后置: $wgActionPaths[$action] = "/wiki/$1/$action"; } $wgActionPaths['view'] = "/wiki/$1"; $wgArticlePath = $wgActionPaths['view'];
- 操作前置:“
http://wiki.eijux.com/wiki/edit/首页
”; - 操作后置:“
http://wiki.eijux.com/wiki/首页/edit
”;
- 虚拟路径:
$actions = array( 'view', 'edit', 'watch', 'unwatch', 'delete','revert', 'rollback', 'protect', 'unprotect', 'markpatrolled', 'render', 'submit', 'history', 'purge', 'info' ); foreach ( $actions as $action ) { #操作前置: $wgActionPaths[$action] = "$wgScriptPath/action/$action/$1"; #操作后置: $wgActionPaths[$action] = "$wgScriptPath/action/$1/$action"; } $wgActionPaths['view'] = $wgScriptPath/action/$1"; $wgArticlePath = $wgActionPaths['view'];
- 操作前置:“
http://wiki.eijux.com/wiki/action/edit/首页
”; - 操作后置:“
http://wiki.eijux.com/wiki/action/首页/edit
”;
- 以上均可分为“操作前置”和“操作后置”。
- 以上均设置单独将 view 操作链接中的“view”关键字省略。
配置了以上内容,仍然需要在“站点的 Ngnix 配置文件中”(example.com.conf)中定义规则以完成 URL 重写。
但是,使用“$wgActionPaths”设置后,不能创建某些与“操作关键字”(如上“$actions
”中定义)相关的页面。
如:创建名为“首页/edit”(action后置)、“edit/首页”(action后置)、“首页/history”的页面时,会跳转到“首页”相关的操作页面。
对此,可以修改“$actions”定义的操作词为不常用词(可能需要修改页面代码?)。
备注
当前站点使用的方式为:example.com/Page_title,现备注正在使用的“wiki.eijux.com.conf”如下。
【2022/08/23 05:41:49】
- LocalSettings.php:
# Short URL $wgArticlePath = "/$1"; $wgUsePathInfo = true; $wgScriptExtension = ".php";
- wiki.eijux.com.conf:【关键部分已高亮】
server { ... 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; }
- 关于fastcgi的配置,参见“/www/server/nginx/conf”中的“enable-php-73.conf”。
- “fastcgi_params”和“fastcgi.conf”均位于“/www/server/nginx/conf”中。
FAQ
404 Not Found nginx
在修改短链接之后,可能出现 404 Not Found nginx 的错误:![]()
出现此错误,其实是“站点的 Ngnix 配置文件”(example.com.conf)配置有问题:
- 若在访问所有页面均出现此错误,则参考 #内容短链接 进行修改即可。
- 若仅访问“png、jpg”、“js、css”等内容时,出现此错误,则修改如下:
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; }
若有其他内容错误,解决类似。
参考
- ↑ Cool URIs don't change
- ↑ 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
- ↑ 7.0 7.1 站点的 Nginx 配置:
1、可以通过“宝塔面板”修改(网站 -> 站点设置 -> 配置文件);
2、也可以通过服务器上“配置文件”修改(位置:“/www/server/panel/vhost/nginx/example.com.conf”)。 - ↑ 8.0 8.1 修改 Nginx 配置之后,可能需要重载 Nginx 配置:
1、在宝塔面板的“Nginx 管理”中“重载配置”;
2、通过命令。 - ↑ 参考:Manual:Short URL/Page title - nginx, Root Access, PHP as a CGI module
- ↑ “操作”包括:页面的“编辑、移动、删除、保护、历史”等等
- ↑ 参考:Manual:$wgActionPaths