Jump to content

Topic on Project:Support desk

Redirect to log in page if user not logged in

2
Rakon12 (talkcontribs)

Hello

I'm creating a private wiki. My goal is to have the user immediately redirected to the login page from any page when they're not logged in. But I cannot get it to work without changing the mediawiki sources. I do not want to change the sources for security reasons.

My versions are:

MediaWiki 1.39.4
PHP 8.2.4 (apache2handler)
MariaDB 10.4.28-MariaDB
ICU 71.1

I tried to use a hook but since the not logged in page is a permissionErrorPage the redirect is not executed. The hooks I tried were BeforeInitialize and BeforePageDisplay.

Below my hook handle which i wrote into LocalSettings.php:

$wgHooks['BeforeInitialize'][] = function ( $title, $unused, $output, $user, $request, $mediaWiki ) {
    # Check if the user is not logged in and not login Page
    if ( $user->isAnon() && 
         strcmp($title, SpecialPage::getTitleFor( 'Userlogin' ))) {
        # Get URL
        $title = SpecialPage::getTitleFor( 'Userlogin' );
        $url = $title->getFullURL();
        # Redirect to login page
        $output->redirect( $url );
        return;
    }
    return true;
};

Is there a way to implement my request without changing the mediawiki sources? Or a different handle that suits my needs? I already searched through Manual:Hooks.

Thanks in advance

Ammarpad (talkcontribs)

How did you set up the 'private wiki'? If set up correctly you don't need to do anything. That should be the default, and in fact, only behavior; i.e "user [is] redirected to the login page from any page when they're not logged in."

Reply to "Redirect to log in page if user not logged in"