“配置MW:短链接”的版本间差异

来自Wikioe
跳到导航 跳到搜索
(Eijux移动页面MediaWiki:短链接配置MW:短链接,不留重定向)
无编辑摘要
第1行: 第1行:
[[category:MediaWiki]]
[[category:MediaWiki]]
 
http://wiki.eijux.com/index.php?title=首页
== 关于 ==
== 关于 ==
短URL或URL重写会隐藏页面地址中的php文件扩展名。格式如:<code>http://wiki.eijux.com/%E9%A6%96%E9%A1%B5</code>、<code>http://wiki.eijux.com/wiki/%E9%A6%96%E9%A1%B5</code><br/>
短 URL 或 URL 重写会隐藏页面地址中的 php 文件扩展名。格式如:<code>http://wiki.eijux.com/首页</code>、<code>http://wiki.eijux.com/wiki/首页</code><br/>
MediwWiki默认的文章链接格式为如:<code>http://wiki.eijux.com/index.php?title=%E9%A6%96%E9%A1%B5</code><br/>
MediwWiki默认的文章链接格式为如:<code>http://wiki.eijux.com/index.php?title=首页</code><br/>
默认文章操作链接格式如:<code>http://wiki.eijux.com/index.php?title=%E9%A6%96%E9%A1%B5&action=edit</code><br/>
默认文章操作链接格式如:<code>http://wiki.eijux.com/index.php?title=首页&action=edit</code><br/>
'''(短链接的修改参见[https://www.mediawiki.org/wiki/Manual:Short_URL Manual:Short URL]页面,对于MediaWiki设置Short URL的注意事项,以及针对不同服务器环境的配置都十分具体。)'''
'''(短链接的修改参见[https://www.mediawiki.org/wiki/Manual:Short_URL Manual:Short URL]页面,对于MediaWiki设置Short URL的注意事项,以及针对不同服务器环境的配置都十分具体。)'''


== 配置步骤 ==
 
 
=== 关于example.com/Page_title ===
使用域的根目录作为wiki目录,并使url如下所示:<code>example.com/Page_title</code>,但并不是官方建议的方式。因为它可能会导致与其他文件和目录冲突。如:图像位于/images/目录中,则无法访问wiki中名为“images”的页面;并且无法创建控制搜索引擎索引页面的robots.txt.txt文件。(参见[https://www.mediawiki.org/wiki/Manual:Wiki_in_site_root_directory Manual:Wiki in site root directory])<br/>
*'''可能的bug如:[https://phabricator.wikimedia.org/T34621 task T34621]、[https://phabricator.wikimedia.org/T40048 task T40048]。'''<br/>
 
 
但同时这种方式在[https://www.gamepedia.com/ Gamepedia]运行良好。<br/>
目前并没有遇到、涉及上述问题,私人小站也不担心链接变动的SEO影响([https://www.w3.org/Provider/Style/URI Cool URIs don't change])。
 
== 内容短链接:$wgArticlePath ==
'''(此次采用的设置参见[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])'''
'''(此次采用的设置参见[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])'''


第62行: 第72行:
使用BTPanel或命令进行nginx的重载配置。<br/>
使用BTPanel或命令进行nginx的重载配置。<br/>


完成配置。长链接<code>http://wiki.eijux.com/index.php?title=%E9%A6%96%E9%A1%B5</code>、短链接<code>http://wiki.eijux.com/%E9%A6%96%E9%A1%B5</code>均可正常访问。
完成配置。长链接<code>http://wiki.eijux.com/index.php?title=首页</code>、短链接<code>http://wiki.eijux.com/首页</code>均可正常访问。


== 关于$wgActionPaths ==
== 操作短链接:$wgActionPaths ==
设置以上<code>$wgArticlePath</code>等设置之后,页面的链接变为了短链接(如:<code>http://wiki.eijux.com/%E9%A6%96%E9%A1%B5</code>),但是页面的编辑、移动、删除、历史等页面,仍然是长链接页面(如:<code>http://wiki.eijux.com/%E9%A6%96%E9%A1%B5?action=edit&veswitched=1</code>),若要使操作页面也应用短链接,则应该设置<code>$wgActionPaths</code>。(参见[https://www.mediawiki.org/wiki/Manual:$wgActionPaths#Spam_prevention Manual:$wgActionPaths])<br/>
设置以上<code>$wgArticlePath</code>等设置之后,页面的链接变为了短链接(如:<code>http://wiki.eijux.com/首页</code>),但是页面的编辑、移动、删除、历史等页面,仍然是长链接页面(如:<code>http://wiki.eijux.com/首页?action=edit&veswitched=1</code>),若要使操作页面也应用短链接,则应该设置<code>$wgActionPaths</code>。(参见[https://www.mediawiki.org/wiki/Manual:$wgActionPaths#Spam_prevention Manual:$wgActionPaths])<br/>
如,设置Action在页面之后:
如,设置Action在页面之后:
<syntaxhighlight lang="php" line>
<syntaxhighlight lang="php" line>
第76行: 第86行:
$wgArticlePath = $wgActionPaths['view'];
$wgArticlePath = $wgActionPaths['view'];
</syntaxhighlight>
</syntaxhighlight>
则页面编辑链接为<code>http://wiki.eijux.com/%E9%A6%96%E9%A1%B5/edit</code>。
则页面编辑链接为<code>http://wiki.eijux.com/首页/edit</code>。


* 但是:
* 但是:
*: 使用<code>$wgActionPaths</code>设置,则不能创建某些与<code>$actions</code>中定义的操作词相关的页面,如:创建名为“首页/edit”(action后置)、“edit/首页”(action后置)、“首页/history”的页面时,会跳转到“首页”相关的操作页面。所以不建议使用,或者修改<code>$actions</code>的操作词为不常用词(可能需要修改页面代码?)。
*: 使用<code>$wgActionPaths</code>设置,则不能创建某些与<code>$actions</code>中定义的操作词相关的页面,如:创建名为“首页/edit”(action后置)、“edit/首页”(action后置)、“首页/history”的页面时,会跳转到“首页”相关的操作页面。所以不建议使用,或者修改<code>$actions</code>的操作词为不常用词(可能需要修改页面代码?)。
== 关于example.com/Page_title ==
使用域的根目录作为wiki目录,并使url如下所示:<code>example.com/Page_title</code>,但并不是官方建议的方式。因为它可能会导致与其他文件和目录冲突。如:图像位于/images/目录中,则无法访问wiki中名为“images”的页面;并且无法创建控制搜索引擎索引页面的robots.txt.txt文件。(参见[https://www.mediawiki.org/wiki/Manual:Wiki_in_site_root_directory Manual:Wiki in site root directory])<br/>
*'''可能的bug如:[https://phabricator.wikimedia.org/T34621 task T34621]、[https://phabricator.wikimedia.org/T40048 task T40048]。'''<br/>
但同时这种方式在[https://www.gamepedia.com/ Gamepedia]运行良好。<br/>
目前并没有遇到、涉及上述问题,私人小站也不担心链接变动的SEO影响([https://www.w3.org/Provider/Style/URI Cool URIs don't change])。


== FAQ ==
== FAQ ==

2022年8月23日 (二) 04:35的版本

http://wiki.eijux.com/index.php?title=首页

关于

短 URL 或 URL 重写会隐藏页面地址中的 php 文件扩展名。格式如:http://wiki.eijux.com/首页http://wiki.eijux.com/wiki/首页
MediwWiki默认的文章链接格式为如:http://wiki.eijux.com/index.php?title=首页
默认文章操作链接格式如:http://wiki.eijux.com/index.php?title=首页&action=edit
(短链接的修改参见Manual:Short URL页面,对于MediaWiki设置Short URL的注意事项,以及针对不同服务器环境的配置都十分具体。)


关于example.com/Page_title

使用域的根目录作为wiki目录,并使url如下所示:example.com/Page_title,但并不是官方建议的方式。因为它可能会导致与其他文件和目录冲突。如:图像位于/images/目录中,则无法访问wiki中名为“images”的页面;并且无法创建控制搜索引擎索引页面的robots.txt.txt文件。(参见Manual:Wiki in site root directory


但同时这种方式在Gamepedia运行良好。
目前并没有遇到、涉及上述问题,私人小站也不担心链接变动的SEO影响(Cool URIs don't change)。

内容短链接:$wgArticlePath

(此次采用的设置参见Manual:Short URL/Page title - nginx, Root Access, PHP as a CGI module

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;
    }

重载nginx配置

使用BTPanel或命令进行nginx的重载配置。

完成配置。长链接http://wiki.eijux.com/index.php?title=首页、短链接http://wiki.eijux.com/首页均可正常访问。

操作短链接:$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的操作词为不常用词(可能需要修改页面代码?)。

FAQ

修改短链接之后,图片页404

以上,修改站点短链接之后,网页、特殊页面均无问题,只有png、jpg等图片页访问404(图片列表、svg页无问题)。
即:站点的nginx配置文件中配置的类型,无法访问:

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        ...
    }
    
    location ~ .*\.(js|css)?$
    {
        ...
    }

需要在Location中配置try_files

try_files 和 $uri

需要在配置中添加try_files $uri @rewrite;
关于nginx配置中的:

  1. try_files尝试读取文件(读取静态文件),按后面的配置一次尝试。
  2. $uringinx的一个变量,存放着用户访问的地址。如:“http://www.xxx.com/index.html”,那么$uri就是 “/index.html”;
  3. $uri/代表访问的是一个目录。如:“http://www.xxx.com/hello/test/”,那么$uri/就是“/hello/test/”;

try_files $uri $uri/ @aaaaa
配置说明:尝试到网站目录读取用户访问的文件,如果$uri存在,就直接返回;不存在继续读取$uri/,如果存在,直接返回;不存在直接跳转到@aaaaa(另一个Location)。

修改配置

如下:

    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.