[Mediawiki-l] Different URL path for different namespace?

Brion Vibber brion at pobox.com
Thu Apr 7 00:42:33 UTC 2011

2011/4/6 Dorem - Jérémie Bouillon <jeremie at dorem.info>

> On 07/04/2011 02:07, Brion Vibber wrote:
> > I think the best way to do this would be to whip up an extension using
> the
> > 'GetLocalURL' hook to change the URLs that MediaWiki outputs when they
> look
> > clean (no extra query params etc).
> I think I understand the theory, what you mean.
> > For general info on extensions&  writing them see:
> > http://www.mediawiki.org/wiki/Manual:Extensions and
> > http://www.mediawiki.org/wiki/Manual:Developing_extensions -- a simple
> > extension can just be one function so it's not too intimidating really.
> :)
> You haven't see what I can write with PHP... it's both intimidating and
> I'll probably add numerous security holes everywhere :p

hehehehe :D Luckily it really is not much code, lemme whip up an example...

You could add this to your LocalSettings.php:

$wgHooks['GetLocalURL'][] = 'efMyCustomLinks';
function efMyCustomLinks( $title, &$url, $query ) {
    if (!$query && $title->getNamespace() == NS_PROJECT) {
        // Rewrite clean links to pages in this namespace
        // Needs mod_rewrite rules or something to match!
        $url = '/proj/' . wfUrlEncode( $title->getDBKey() );
    return true;

The main things you want to customize here are which namespace id key it
compares against (the numeric key, so whatever you're adding to
$wgExtraNamespaces or one of the standard namespace name constants like
NS_USER, NS_TALK etc) and of course the actual output path.

In the Apache rewrite rules, don't forget to add back the namespace prefix
('Foo:' or whatever)!

-- brion

More information about the MediaWiki-l mailing list