1

I have a custom register form page, and I want to create a 301 redirect to force people to use this form (and not the original URL of WordPress)

I try to do a redirect 301 like this in my .htaccess, but it doesn't work.

Redirect 301 /wp-login.php?action=register https://monsite.com/register/

What's wrong here?

3 Answers 3

4

I would advice you to not use the htaccess for this, but to use a WordPress hook instead.

Check out https://developer.wordpress.org/reference/hooks/login_url/

3

I would suggest you follow @DeltaG's advice in their answer. However, to answer your specific question...

Redirect 301 /wp-login.php?action=register https://example.com/register/

This will never match because the Redirect (mod_alias) directive matches against the URL-path only, not the query string.

To perform this redirect in .htaccess you would need to do something like the following using mod_rewrite at the top of the .htaccess file, before the existing WordPress code block (ie. before the # BEGIN WordPress comment marker):

RewriteCond %{QUERY_STRING} ^action=register$
RewriteRule ^wp-login\.php$ https://example.com/register/ [R=301,QSD,L]

The QSD flag is necessary to remove the original query string from the request.

1

Like @DeltaG said, filtering the login_url is a better option than using a .htaccess redirect. If you absolutely need to use .htaccess, follow @MrWhite answer.

Here is an approach using default WordPress hooks..

function wpse404009_redirect_to_custom_registration_page() {

    global $pagenow;
    
    if ( $pagenow == 'wp-login.php' && ! empty( $_REQUEST['action'] ) && $_REQUEST['action'] === 'register' ) {
        //We are on the registration page, redirect now.
        wp_redirect( get_permalink(999), 301 ); ; //Change 999 to the ID of your registration page
        exit;
    }
}
add_action( 'login_init', 'wpse404009_redirect_to_custom_registration_page' );

If you want to filter the login_url.

function wpse404009_custom_login_page( $login_url, $redirect, $force_reauth ) {
    
    //Get the custom login page URL.
    $login_url = get_permalink(999); //Change 999 to the ID of your login page
    
    if ( !empty($redirect)) {
        $login_url = add_query_arg( 'redirect_to', urlencode( $redirect ), $login_url );
    }
    return $login_url;
}
add_filter( 'login_url', 'wpse404009_custom_login_page', 10, 3 );
2
  • Thanks. I tryied for the register, but it looks like the code is not executed. Any idea of why ? I put a big exit at the beginning to be sure and nothing happen
    – djoo
    Commented Mar 23, 2022 at 15:43
  • @djoo My bad, hooked into a action that don't run on login page. Fixed the code, try now. Remember, the first chunk of code works only on registration page. If you want both the login and registration page to redirect, just delete the $_REQUEST['action'] check.
    – Abhik
    Commented Mar 24, 2022 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.