Projects Engine
  • Home
  • Blog
  • Invoice
  • Courses
  • Contact
  • Login
  • Home
  • 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 3 minutes read

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();
headerswordpress
Facebook Twitter linkedin

Was this article helpful?

Yes No

Post navigation

Previous post
Next post

Subscribe

Subscribe to our mailing list to get the new updates.

Follow us

Latest posts

  • How to create a Log in with GitHub button for users to log in on your website in PHP

    How to create a Log in with GitHub button for users to log in on your website in PHP

    26.03.2023
  • How to modify menu items and menu locations in WordPress in various ways

    How to modify menu items and menu locations in WordPress in various ways

    21.03.2023
  • How to convert an array to a comma-separated list in PHP in various ways

    How to convert an array to a comma-separated list in PHP in various ways

    20.03.2023

Popular posts

  • How to convert an array to a comma-separated list in PHP in various ways

    How to convert an array to a comma-separated list in PHP in various ways

    20.03.2023
  • How to Edit the WooCommerce Single Product Template Programmatically with Examples

    How to Edit the WooCommerce Single Product Template Programmatically with Examples

    13.03.2023
  • How to include variables from PHP to a JS file in WordPress

    How to include variables from PHP to a JS file in WordPress

    18.03.2023
Projects Engine
Projects Engine provides code and tutorials for your themes and plugins you need to create remarkable sites on WordPress that will drive your business forward faster. The primary purpose of this site is to give high-quality WordPress tips, tricks, hacks, and other tools to help WordPress developers improve their site(s) and skills. Projects Engine was founded in December 2020 by Dragi Postolovski.
Copyright 2023 Projects Engine
  • Home
  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
  • Courses
  • Dictionary
  • Plugins
  • Contact

Our website uses cookies to improve your experience. Learn more

I understand!