Tutorial:Beautiful MediaWiki URLs

De Wiki Seb35
Révision datée du 27 novembre 2014 à 01:36 par Seb35 (discussion | contributions) (first part)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Aller à la navigation Aller à la recherche

This page is about configurating beautiful, pure URLs for MediaWiki, basically without any visible "index.php". The reasoning is that this syntax "index.php" is not interesting from the user’s point of view, and it’s ugly, so it shouldn’t be there.

This wiki implements these rules. Technically, this comes from CGI scripts, and this have to be taken into account to configure the server. Thereafter is the configuration to achieve that with nginx.


  • A server
  • MediaWiki on this server in the directory, say, '/mediawiki'
  • nginx, preferably with the module Lua

MediaWiki configuration

In the file LocalSettings.php, add the following directives at the end.

Remove the "script path" to remove all subdirectories.

$wgScriptPath = ;

Set the article path to its simplest form. With this, links to view the articles will be beautiful.

$wgArticlePath = '/$1';

Now, true improvements begin. When you view an article, the URL is beautiful, but it becomes again ugly when you edit an article or ask its printable form. So let’s remove the "index.php".

$wgScript = ;

The links for the actions have the form "/Main_Page?title=Main_Page&action=edit" for the edit action. You can remark the title is displayed twice. To remove this, we have to add a small hook in LocalSettings.php to remove the "title=" parameter.

$wgHooks['GetLocalURL::Internal'][] = 'urlPhpAgnostic';
function urlPhpAgnostic( &$title, &$url, $query ) {
        global $wgArticlePath;

        $url = str_replace( '$1', wfUrlencode( $title->getPrefixedDBkey() ), $wgArticlePath );

        while ( preg_match( '/^(.*&|)title=([^&]*)(&.*|)$/', $query, $matches ) ) {
                $query = $matches[1] . $matches[3];

        if ( $query !=  ) {
                $url = wfAppendQuery( $url, $query );