Removing index.php from MediaWiki action URLs

核能气质少年 提交于 2019-12-12 13:20:27

问题


OK so its fairly well documented how to hide the index.php?title= bit from urls, but what I would like is to hide it for all the action type links, similar to how the extension ShortUrls is supposed to work (It doesn't on my site and I don't know how to fix it). I don't have access to .htaccess and was thinking of doing it by adding some javascript to the common.js page, or by modifying the ShortLinks extension but the documentation on those hooks isn't very good and anything I tried wasn't helping.


回答1:


Basically, you need to add an entry to $wgActionPaths for each action that you want to use a short URL for.

For example, if you want the normal view URL of the page Foobar to be /wiki/Foobar, and the edit and history URLs to be, say, /wiki/edit/Foobar and /wiki/history/Foobar, you'd add the following lines to your LocalSettings.php:

$wgArticlePath = '/wiki/$1';
$wgActionPaths['edit'] = '/wiki/edit/$1';
$wgActionPaths['history'] = '/wiki/history/$1';

Of course, you'll also need to configure your web server to rewrite any requests for those short URLs back into something MediaWiki will understand, e.g. using mod_rewrite on Apache. The documentation page I linked to above has some helpful examples.


More generally, you can transform MediaWiki-generated URLs in arbitrary ways using a GetLocalURL or GetLocalURL::Internal hook. (The main difference between the two hooks is that the GetLocalURL hook is also called for interwiki URLs.) These hooks are called from the Title::GetLocalURL(), which you may want to take a look at to see how they work.

For example, here's how you could transform diff URLs into the format /wiki/diff/revA/revB/Page_name:

function prettyDiffURLs( $title, &$url, $query ) {
    if ( preg_match( '/^diff=(\w+)&oldid=(\w+)$/', $query, $matches ) ) {
        $dbkey = wfUrlencode( $title->getPrefixedDBkey() );
        $url = "/wiki/diff/$matches[1]/$matches[2]/$dbkey";
    }
    return true;
}
$wgHooks['GetLocalURL::Internal'][] = 'prettyDiffURLs';

(Warning: I believe this code should work, but I have not tested it!)




回答2:


My eventual answer (thanks to the above answer) is:

function pretty_diff($title, &$url, $query)
{
        if ( preg_match('/diff=(\w+)&oldid=(\w+)/', $query, $matches))
        {
                $dbkey = wfUrlencode($title->getPrefixedDBkey());
                $url = "/wiki/$dbkey?$matches[0]";
        }
        return true;
}
$wgHooks['GetLocalURL::Internal'][] = 'pretty_diff';

but I eventually replaced it with a javascript function to potentially cover more links



来源:https://stackoverflow.com/questions/22844653/removing-index-php-from-mediawiki-action-urls

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!