Home How to edit the X-Redirect-By header using the Redirection Plugin Developers How to edit the X-Redirect-By header using the Redirection Plugin How to edit the X-Redirect-By header using the Redirection Plugin

How to edit the X-Redirect-By header using the Redirection Plugin

November 04, 2022
How to edit the X-Redirect-By header using the Redirection Plugin

Redirection plugin

The most well-liked redirect manager for WordPress is called Redirection. It makes it simple to handle 301 redirects, monitor 404 issues, and generally take care of any remaining loose ends on your website. This might reduce problems and raise the rating of your website.

X-Redirect-By

There should be an X-Redirect-By header with an identifier as its value just before the location header.

Redirects

As you already know the plugin offers a lot of possibilities for creating redirects. One of them is using regex expressions.

For example: I want every link that is found on my website with this expression ^\/(nl|nb-no|pt-pt|sv|da-dk|fi)\/projects\/popular-projects(\/)?(\?.*)? to be redirected to this link /$1/projects/$3. This expression has an id and other information.

When a user clicks on a link that matches the regex expression it will be redirected to the redirection link. In the headers of the redirection link you will find information about the X-Redirect-By which by default is named redirection.

We will change it a little bit and maybe we can add information like an id so we know what expression is used for that redirect to happen. Let’s get started.

Hooks

 add_action( 'redirection_matched', array( $this, 'pe_intercept_redirects' ), 9, 3 );
 add_filter( 'x_redirect_by', array( $this, 'pe_x_redirect_by_filter' ), 10, 3 );

These two hooks can be used to get the result we explained earlier. I suggest using a class or maybe a global variable so it can be initialized in the first hook and sent to the second hook. We will be using a class.

Create a class and in the constructor add the two hooks. After that, create two methods in your class.

public function pe_intercept_redirects( $url, $item, $redirects ) {
    $this->redirectID = (string)$item->get_id();
}

The second parameter is consisted of all the information you need to know about the redirect. One of those information is the expression id used for the redirect. Assign the id to a variable. Convert the variable to string because the X-Redirect-By header receives only strings.

public function pe_x_redirect_by_filter( $x_redirect_by, $status, $location ) {
    return $this->redirectID;
}

Return the assigned variable from the previous hook. If successful you should be able to see the id in the headers.

How to edit the X-Redirect-By header using the Redirection Plugin

Now you know which expression was used for the redirect to happen.

Important

In order for this to work redirection_matched must be executed first before the x_redirect_by. You can easily check the hooks order using the Query Monitor plugin.

<?php

class PE_ManageRedirects {

    private $redirectID;

    public function __construct() {
        $this->load_hooks();
    }

    public function load_hooks() {
        add_action( 'redirection_matched', array( $this, 'pe_intercept_redirects' ), 9, 3 );
        add_filter( 'x_redirect_by', array( $this, 'pe_x_redirect_by_filter' ), 10, 3 );
    }

    public function pe_intercept_redirects( $url, $item, $redirects ) {
        $this->redirectID = (string)$item->get_id();
    }

    public function pe_x_redirect_by_filter( $x_redirect_by, $status, $location ) {
        return $this->redirectID;
    }
}

new PE_ManageRedirects();
Was this article helpful?