Support » Plugin: Redirection » Infinite loop when renaming A -> B -> C -> A

  • If you rename a post slug from A to B and back A, this line in the code correctly prevents an infinite redirect:

    $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_items WHERE url=%s AND action_type=%s AND action_data=%s", $data['action_data'], $data['action_type'], $data['url'] ) );

    However, if you rename it from A to B to C and then back to A, that line doesn’t apply and you get an infinite loop. It’s less common than the simple case, but still I’ve had this happen to my clients a few times.

    I am currently using this workaround in my functions.php, which only deletes redirects in the “Modified Posts” group to ensure it doesn’t delete any custom redirects:

    add_action('post_updated', function($post_id, $post, $post_before)
    {
        global $redirection, $wpdb;
    
        $options = $redirection->get_options();
    
        if ($options['monitor_post'] > 0 && get_option('permalink_structure')) {
            $url = parse_url(get_permalink($post_id), PHP_URL_PATH);
            $redirects = Red_Item::get_for_url($url, 'wp');
            foreach ($redirects as $redirect) {
                // We're only looking for redirects automatically created by the
                // Redirection plugin - don't delete ones in other groups. Also
                // ignore any regex redirects because they haven't been filtered at
                // this point, but we can't call $redirect->matches($url) to check
                // if it matches because that immediately follows the redirect. (Not
                // really a problem because there shouldn't be any regex redirects
                // in this group.)
                if ($redirect->group_id == $options['monitor_post'] && !$redirect->regex) {
                    Red_Item::delete($redirect->id);
                }
            }
        }
    }, 12, 3);

    It would be good if the plugin could do something similar.

    (Note: This code only works with the WordPress module, not Apache, but it could be modified to work with both.)

    https://wordpress.org/plugins/redirection/

  • The topic ‘Infinite loop when renaming A -> B -> C -> A’ is closed to new replies.