DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:redirect

Redirect Plugin

Compatible with DokuWiki

  • 2024-02-06 "Kaos" unknown
  • 2023-04-04 "Jack Jackrum" unknown
  • 2022-07-31 "Igor" yes
  • 2020-07-29 "Hogfather" yes

plugin Redirects page accesses to other pages or external sites using a central configuration file

Last updated on
2019-12-04
Provides
Admin, Action
Repository
Source

Download and Installation

Search and install the plugin using the Extension Manager. Refer to Plugins on how to install plugins manually.

Changes

Configuration/Usage

The plugin stores a list of page/redirect pairs in a config file called redirect.conf in DokuWiki's conf/ directory. This file needs to be writable by the webserver to edit it from the administration menu, under section Additional Plugins.

In order to implement the redirections manually, just place on the redirect.conf file lists of the old pages, followed by the new ones:

old_namespace:my_page new_namespace:my_page

On this example, when the users try to access old_namespace:my_page it will be redirected to new_namespace:my_page

Access to the configuration is granted for Managers and Superusers.

You may tell the plugin to show an info message to users when they were redirected. This can be done in the Config Manager.

Comments

301 redirects

If we change the following line in inc/common.php

header('Location: '.$url);

to

header('Location: '.$url,TRUE,301);

then instead of a 302 response - the default when no $http_response_code parameter is specified in header() - we get a 301 which is supposed to be better for SEO reasons.

Is such a change, or for that matter a modification of send_redirect() so that one can optionaly specify an $http_response_code other than 302, a good idea?

Wow! It handles redirects to external URLs too!

Thanks!
But I see no way to update redirect.conf from the Config manager…
Manual editing is ok. — jno 2011/07/08 13:27

3 years later (Ponder Stibbons release) there is a menu entry on the “Admin” page to configure the plugin. It contains the text of $lang['name']. It's not in the “redirect” part of the config manager… — Werner Flamme 2014-06-02 15:25 CEST

Wildcard redirection

A simple modification will add an ability to redirect whole namespaces (and even more) using simple wildcard syntax in config file:

myteam:*        contacts:mycompany:mydepartment:myteam:$1
client:*        contacts:clients:clientname:$1
server:*        it:system_dpt:resources:servers:$1

The fix:

Edit helper.php file:

Find this line:

 if(empty($redirects[$id])) return false;

Place this code before that line:

 $newID = "";
 foreach ( $redirects as $mask=>$target )
      {
       $regex_mask = '/^'.preg_replace( '/\*/', '(.*)', $mask ).'/';
       if ( preg_match( $regex_mask, $id, $matches ) )
       {
               $newID = preg_replace( $regex_mask, $target, $id );
               break;
       }
      }

 $redirects[$id] = $newID;

Note that you'll need to symlink the directories in data/pages if you want internal links to show correct page status (available/missing).
— Michał Sacharewicz 2012/03/15 11:04

This almost does what I need it to. I'm organizing my novel notes in dokuwiki and have it broken out into namespaces. I want to be able to quickly add [[character_name]] and have it properly link to characters:protagonists:character_name. But, if I link to the character from places:town, it creates the link to places:character_name, which is obviously not where the page is. If I set up the redirect with *character_name redirecting to characters:protagonists:character_name, it does indeed redirect in every applicable instance. Unfortunately, it creates an infinite loop because the ultimate destination also contains character_name. Is there a way to modify this so that it will exclude the destination page? My PHP isn't really up to snuff. Or if there's another plugin that will have the same effect. I just don't want to create individual redirect pages for every other page in every namespace. But I really just want to be able to type the name and throw a few brackets around it without have to type in the whole name with namespaces. — Matt 2014/8/27 05:56

The fix for the fix - avoid redirect loops

(also called the collaborated double fix)

The * Wildcard modification works quite well despite some redirect loops.

To avoid redirect loops we added a second parameter redirected to the created URL in helper.php.

and in action.php we check for this parameter.

This is a quick hack and straight forward.

In helper.php:

add $url.=“&redirected=1”;

to the code at the very end of the file just right before return $url;

In action.php:

find the function handle_start at end of the file and add

if($INPUT→get→str('redirected') == '1') return;

right after or before

if($INPUT→get→str('redirect') == 'no') return;

now you can do something like this (see below) which without the modification will end up in an infinite loops…. now it works magically…

index:a* index:a:a$1: or even index:a-*b* index:ab:a-$1b$2:

(you may have to play around a bit to get the effect…..)

Note: you cant use $0, you have to use $1 as a placeholder - but as the searchterm is stripped from $1, you have to add the searchterm before the placeholder in the target definition

(in the example above it's the a*/a$1: or a*b*/a$1b$2:)

In the example above we redirect all pages start with a to a namespace of the same name in namespace a…. or all pages starting with a-*b* to ab:a-*b*

sounds a bit weird ;) - it is!

Best regards Volker & Lucas - 2022-03-29

Virtual Hostname Redirects

I needed something to redirect based on virtual hostname and path matches so I made a modification to let me do that with this plugin. Here are the changes I made in the action.php file.

 function handle_start(&$event, $param){
        global $ID;
        global $ACT;

        if($ACT != 'show') return;

        $redirects = confToHash(dirname(__FILE__).'/redirect.conf');
		$landing = $redirects[$ID];
		if( empty($landing) ) { $landing = $redirects[ $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']]; }
        if($landing){
            if(preg_match('/^https?:\/\//',$landing)){
                send_redirect($landing);
            }else{
                if($this->getConf('showmsg')){
                    msg(sprintf($this->getLang('redirected'),hsc($ID)));
                }
                $link = explode('#', $landing, 2);
                send_redirect(wl($link[0] ,'',true) . '#' . rawurlencode($link[1]));
            }
            exit;
        }
    }

If I add a line in the redirect.conf that says

workorders.mysite.com/ admin:workorders:start

anyone who comes in with that dns name at the root of my site will get redirected to the admin:workorders:start page.
— Matthew Mills 2013/07/05

Redirection based on group of logged in user

Is it possible to tweak this plugin to make it redirect a page to another page based on the group of the logged in user? In other words, I would like the target page to change depending on the group the user belongs to.

Thanks in advance. – Francesco 2013/09/12

Redirection to anchors

Hi there! Redirects like page another_page#anchor work just like page another_page I would like you to correct this behaviour.

It's said in the instruction that anchor hash should be escaped with a backslash \. — MilchFlasche 2018-04-06 20:58

Bug in Detritus

In the latest version of DokuWiki, redirects always add a hash at the end of the URL. This didn't happen with the previous version. — zioth 2015-08-14 03:45

Please report bugs here: https://github.com/splitbrain/dokuwiki-plugin-redirect/issues

Media redirects

Please add second admin config window for media's. Ex:

  • page de/folders/index
  • has an image with syntax {{header.jpg}}
  • but we don't use _media/de/folders/
  • so image redirects by media-rule *:very:long:* very:long:$2
  • to correct path _media/folders/header.jpg

Rewrited by root .htaccess with such rules (only for 2-letter folders!):
RewriteRule ^_media/(..)/(.*) lib/exe/fetch.php?media=$2 [QSA,L]
RewriteRule ^_media/(.*) lib/exe/fetch.php?media=$1 [QSA,L]

KISS principle as is.

plugin/redirect.txt · Last modified: 2022-11-21 00:26 by Aleksandr

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki