Open main menu

Help:Setting up client-side redirects

It is possible to set up MediaWiki to force client-side redirects (using HTTP 302, "temporary redirect"). Doing this means redirects will be handled by the client's browser instead of by the server. Two methods exist to do this.

Contents

Method 1Edit

for MediaWiki 1.9.0)

What?
This piece of code forces all redirects to be done by the client, by issuing them a 302 Temporary Redirect to the new pages specific location.
Why?
A lot of search engines decrease your pages rank if they discover duplicated content. The traditional Redirect method allows a huge amount of duplication, and doesn't redirect as nicely. This also allows you to use images for navigation. Additionally it can be a limitation/annoying to not see the full redirected pages location in the address bar, and having the annoying "Redirected from" link.
Warning: This hack will increase your servers network load somewhat, for 99% of MediaWiki servers this will be negligible, but I wouldn't see Wikipedia doing this mod any time soon!
How?
Open "includes\Article.php" and edit the function "followRedirect()"
Change line 94 (approximately) from "return $rt;" to "return $rt->getFullURL();"
The whole function should now look like this:
	/**
	 * @return mixed false, Title of in-wiki target, or string with URL
	 */
	function followRedirect() {
		$text = $this->getContent();
		$rt = Title::newFromRedirect( $text );
		
		# process if title object is valid and not special:userlogout
		if( $rt ) {
			if( $rt->getInterwiki() != '' ) {
				if( $rt->isLocal() ) {
					// Offsite wikis need an HTTP redirect.
					//
					// This can be hard to reverse and may produce loops,
					// so they may be disabled in the site configuration.
					
					$source = $this->mTitle->getFullURL( 'redirect=no' );
					return $rt->getFullURL( 'rdfrom=' . urlencode( $source ) );
				}
			} else {
				if( $rt->getNamespace() == NS_SPECIAL ) {
					// Gotta handle redirects to special pages differently:
					// Fill the HTTP response "Location" header and ignore
					// the rest of the page we're on.
					//
					// This can be hard to reverse, so they may be disabled.
					
					if( $rt->isSpecial( 'Userlogout' ) ) {
						// rolleyes
					} else {
						return $rt->getFullURL();
					}
				}
				return $rt->getFullURL();
			}
		}
		
		// No or invalid redirect
		return false;
	}

Method 2Edit

Since modifying MediaWiki's functions like done in the method mentioned above could break some extensions using them like Polyglot, you should better use the following little extension:

	<?php
	$wgHooks['InitializeArticleMaybeRedirect'][] = 'redirectHook';
	
	function redirectHook($title, $request, &$ignoreRedirect, &$target, &$article)
	{
		if (!$ignoreRedirect && $article->isRedirect()) {
			if (($target = $article->followRedirect()) instanceof Title) {
				$target = $target->getFullURL();
			}
		}
		return true;
	}

It works for all Version since 1.14.0. The Hook InitializeArticleMaybeRedirect was actually introduced in 1.13.0, but the $article parameter used was missing back then. If you're using a version prior to 1.14.0, you could insert the function body directly at the position in includes/Wiki.php, where the hook was placed later, but you really need to know what you're doing then.