This post has been updated 2015-09-06 with simplified code suggested by Krinkle and again in 2017-04-04.
Google PageSpeed Insights writes:
Redirects trigger an additional HTTP request-response cycle and delay page rendering. In the best case, each redirect will add a single round trip (HTTP request-response), and in the worst it may result in multiple additional round trips to perform the DNS lookup, TCP handshake, and TLS negotiation in addition to the additional HTTP request-response cycle. As a result, you should minimize use of redirects to improve site performance.
Let’s consider the situation where you run MediaWiki as the main thing on your domain. When user goes to your domain example.com
, MediaWiki by default will issue a redirect to example.com/wiki/Main_Page
, assuming you have configured the recommended short urls.
In addition the short url page writes:
Note that we do not recommend doing a HTTP redirect to your wiki path or main page directly. As redirecting to the main page directly will hard-code variable parts of your wiki’s page setup into your server config. And redirecting to the wiki path will result in two redirects. Simply rewrite the root path to MediaWiki and it will take care of the 301 redirect to the main page itself.
So are we stuck with a suboptimal solution? Fortunately, there is a way and it is not even that complicated. I will share example snippets from translatewiki.net configuration how to do it.
Configuring nginx
For nginx, the only thing we need in addition the default wiki
short url rewrite is to rewrite /
so that it is forwarded to MediaWiki. The configuration below assumes MediaWiki is installed in the w
directory under the document root.
location ~ ^/wiki/ { rewrite ^ /w/index.php; } location = / { rewrite ^ /w/index.php; }
Whole file for the curious.
Configuring MediaWiki
First, in our LocalSettings.php
we have the short url configuration:
$wgArticlePath = "/wiki/$1"; $wgScriptPath = "/w";
In addition we use hooks to tell MediaWiki to make / the URL for the main page, not to be redirected:
$wgHooks['GetLocalURL'][] = function ( &$title, &$url, $query ) { if ( $title->isExternal() && $query != '' && $title->isMainPage() ) { $url = '/'; } }; // Tell MediaWiki that "/" should not be redirected $wgHooks['TestCanonicalRedirect'][] = function ( $request ) { return $request->getRequestURL() !== '/'; };
This has the added benefit that all MediaWiki generated links to the main page point to the domain root, so you only have one canonical url for the wiki main page. The if block in the middle with strpos
checks for problematic characters ?
and &
and forces them to use the long URLs, because otherwise they would not work correctly with this nginx rewrite rule.
And that’s it. With these changes you can have your main page displayed on your domain without redirect, also keeping it short for users to copy and share. This method should work for most versions of MediaWiki, including MediaWiki 1.26 which forcefully redirects everything that doesn’t match the canonical URL as seen by MediaWiki.