Extension:Redirect302

From MediaWiki.org
(Redirected from User:Joshuagay/Redirect302)
Jump to: navigation, search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png
Redirect302

Release status:Extension status beta

ImplementationTemplate:Extension#type Hook
DescriptionTemplate:Extension#description Adds a hook to create 302 style redirects
Author(s)Template:Extension#username Joshua Gay (Joshuagaytalk)
Latest versionTemplate:Extension#version 0.2 (2012-11-07)
MediaWikiTemplate:Extension#mediawiki 1.17+
Database changesTemplate:Extension#needs-updatephp No
LicenseTemplate:Extension#license GPL
Download see below
ExampleTemplate:Extension#example The Free Software Directory
Hooks usedTemplate:Extension#hook
InitializeArticleMaybeRedirect

Translate the Redirect302 extension if it is available at translatewiki.net

Check usage and version matrix.

The Redirect302 extension changes the redirect hook so that an http 302 error is issued and the redirect is done on the client side.

Installation[edit]

  • Copy the code into files and place the file(s) in a directory called Redirect302 in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
require_once "$IP/extensions/Redirect302/Redirect302.php";
  • YesY Done - Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Code[edit]

Redirect302.php[edit]

<?php

/**                                                                                                             
 * This extension changes the redirect hook so that an http 302 error                                           
 * is issued and the redirect is done on the client side.
 *
 * @file
 * @ingroup Extensions                                                       
 */

if ( !defined( 'MEDIAWIKI' ) ) die();

// credits                                                                                                      
define('Redirect302_VERSION', '0.2' );
$wgExtensionCredits['other'][] = array(
         'path' => __FILE__,
         'name' => 'Redirect302',
         'version' => Redirect302_VERSION,
         'author' => array( 'Joshua Gay' ),
         'url' => 'https://www.mediawiki.org/wiki/Extension:Redirect302',
         'descriptionmsg' => 'redirect302-desc',
);

// messages i18n                                                                                                
$dir = dirname(__FILE__) . '/';
$wgExtensionMessagesFiles['Redirect302'] = $dir . 'Redirect302.i18n.php';

// Register hook                                                                                                
$wgHooks['InitializeArticleMaybeRedirect'][] = 'redirect302_hook';

// Redirect with 302                                                                                            
function redirect302_hook($title, $request, &$ignoreRedirect, &$target, &$article) {
  if (!$ignoreRedirect && $article->isRedirect()) {
    if(($target = $article->followRedirect()) instanceof Title) {
      $target = $target->getFullURL();
    }
  }
  return true;
}

Redirect302.i18n.php[edit]

<?php
/**
 * Internationalisation file for extension Redirect302.
 *
 * @file
 * @ingroup Extensions
 */

$messages = array();
 
/** English
 * @author Joshua Gay
 */
$messages['en'] = array(
         'redirect302-desc' => 'Adds a hook to create 302 style redirects',
);

/** German (Deutsch)
 * @author Kghbln
 */
$messages['de'] = array(
         'titlekey-desc' => 'Ermöglicht Umleitungen mit HTTP-Statuscode 302',
);