skip to Main Content

a redirect_to from the wordpress software isn’t working

domain.com/wp-login.php?redirect_to=https%3A%2F%2Fforums.domain.com%2F

which should then return to the sub directory of forums

However it simply goes to domain.com/wp-admin

I have disabled all plugins, tried a different browser, tried both an admin and regular user.

I have also upgraded to 6.3.1 – tried on both this and the old version.

I run cloudfare, but also disabled this.

I also have a redirection plugin installed – but this works fine (also disabled to check a conflict)

I am at a loss to figure out why this is failing

2

Answers


  1. Chosen as BEST ANSWER

    The above didn't work.

    instead

        function my_allowed_redirect_hosts( $hosts ) {
        $my_hosts = array(
            'forums.domain.com',
            'www.domain.com',
            'domain.com',
        );
        return array_merge( $hosts, $my_hosts );
    };
    add_filter( 'allowed_redirect_hosts', 'my_allowed_redirect_hosts' );
    

    should work.

    The difference being the word array missing in the first example.


  2. If redirect_to is passed via url request, WordPress uses the function wp_safe_redirect (not wp_redirect) which checks for allowed hosts for security reasons. As forums.domain.com is different from domain.com and is not in the list of allowed hosts, the redirect defaults to admin_url().

    To make the redirect work, you should add forums.domain.com to the list of allowed hosts (e.g. inside your functions.php) using a filter function:

    function my_allowed_redirect_hosts( $hosts ) {
        $my_hosts = [
            'domain.com',
            'www.domain.com',
            'forums.domain.com',
        ];
        return array_merge( $hosts, $my_hosts );
    };
    add_filter( 'allowed_redirect_hosts', 'my_allowed_redirect_hosts' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search