Support Log in

22. Redirect Manager

Developer Guide
Prefer video? Watch the Rank Forge tutorials 9 short guides covering every feature Watch

The redirect manager handles 301, 302 and 307 redirects on exact paths, auto-creates a 301 redirect when a published post’s slug changes, and tracks 404 errors. The Redirect Manager screen offers 301 Permanent, 302 Temporary and 307 Temporary. A rule matches the request path exactly, leading and trailing slashes aside, and the query string is not part of the match; there are no regex or wildcard rules. A rule whose target is on another domain is followed only when its allow_external value is 1. The screen sets it with the checkbox Allow redirect to external domain (off-site) of the Add Redirect form; without the tick the screen refuses to save a target on another domain and shows an error. A rule with a target on another domain that code stores with add() and no $allow_external is saved, but the visitor is not redirected. The Type menu has no 410 choice, but add() stores the type you pass, and a stored rule of type 410 is answered with status 410 Gone and your theme’s 404 template (the theme’s index template when it has no 404 template); its target is not used. The SEOPress import brings a redirect in only when it has a destination, so a 410 rule with an empty destination is not imported. Free core feature.

Database Table Schema

sql
CREATE TABLE wp_rankforge_redirects (
    id            bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    source_url    varchar(500) NOT NULL,
    target_url    varchar(500) NOT NULL DEFAULT '',
    redirect_type int(3) NOT NULL DEFAULT 301,
    hits          int(11) NOT NULL DEFAULT 0,
    last_hit      datetime DEFAULT NULL,
    auto_created  tinyint(1) NOT NULL DEFAULT 0,
    allow_external tinyint(1) NOT NULL DEFAULT 0,
    created_at    datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY source_url (source_url(191)),
    KEY redirect_type (redirect_type)
) /* charset and collation of the site: $wpdb->get_charset_collate() */;

Programmatic Redirect Management

The method is add( $source, $target, $type = 301, $auto = false, $allow_external = false ). It returns the ID of the rule. When a rule for that source exists, it is updated (target, type and $allow_external) and its ID is returned. A target on another domain is stored, and the visitor is redirected to it when $allow_external is true.

php
$redirects = RANKFORGE_Redirects::instance();

// Add a 301 permanent redirect
$id = $redirects->add( 'old-page', 'https://example.com/new-page/', 301, false );

// Add a 302 temporary redirect
$id = $redirects->add( 'promo-2023', 'https://example.com/promo-2024/', 302, false );

// Get the redirects (up to 500, ordered by hits) as an array of objects
$all = $redirects->get_all();

// Get the 404 log
$log = $redirects->get_404_log();
// Returns: [ 'old-page' => 5, 'missing-post' => 2, ... ]

Custom Redirect Rules

php
// Auto-create redirects from 404 log for high-traffic URLs
$log = RANKFORGE_Redirects::instance()->get_404_log();
foreach ( $log as $url => $hits ) {
    if ( $hits >= 5 ) {
        RANKFORGE_Redirects::instance()->add( $url, home_url( '/' ), 301, false );
    }
}

// Delete all auto-created redirects
global $wpdb;
$wpdb->delete(
    $wpdb->prefix . 'rankforge_redirects',
    [ 'auto_created' => 1 ],
    [ '%d' ]
);

// Rank Forge keeps the rules in the object cache and in a transient (one day); clear both so the deletion takes effect
delete_transient( 'rankforge_redirect_map' );
wp_cache_delete( 'rankforge_redirect_map', 'rankforge' );

// Query high-traffic redirects
$high_traffic = $wpdb->get_results(
    $wpdb->prepare(
        "SELECT * FROM {$wpdb->prefix}rankforge_redirects WHERE hits > %d ORDER BY hits DESC LIMIT %d",
        100, 20
    )
);

404 Tracking

When a front-end request ends in a 404, the request path is logged to a transient (rankforge_404_log) with hit counts. Once an address has had 20 of its 404 requests logged, its further 404 requests are skipped until a minute has passed since the 20th. The log keeps the 50 paths with the most hits, and the transient expires one week after the last entry. The Redirect Manager screen lists the top 20 under Recent 404 URLs.

—

Forge AI Assistant Online

Hi! I'm the Rank Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs