---
title: Why WordPress Redirects
date: 2025-03-27T10:33:29+00:00
modified: 2025-11-07T13:27:08+00:00
image:: https://wpelevator.com/wp-content/uploads/sites/12/2025/03/wp-redirect-query-monitor-logs.png
permalink: https://wpelevator.com/guides/log-wordpress-redirects
post_type: page
author:
  name: Kaspars
  avatar: https://secure.gravatar.com/avatar/92bfcd3a8c3a21a033a6484d32c25a40b113ec6891f674336081513d5c98ef76?s=96&d=robohash&r=g
---

# Why WordPress Redirects

Sometimes you see a redirect on a WordPress site and you don’t know what is causing it. Here are ways to determine the source of any redirect.

## How Redirects Work?

There are only two ways a redirect can happen on the web:

1. A web servers instructs the browser to redirect by using literal words — a simple `Location: ...` string in the HTTP response header which browsers immediately use to take you to another *location*.
2. A Javascript function updates the value of `window.location = ...` which causes the browser to redirect.

This guide is focused on detecting the redirects issued by the web server.

## Verify X-Redirect-By Source

[In 2017 Joost de Valk added](https://joost.blog/redirect-by-http-headers/) the `X-Redirect-By` HTTP response header [to the Yoast SEO plugin](https://yoast.com/developer-blog/x-redirect-by-header/) and it was later [included in WordPress core](https://developer.wordpress.org/reference/functions/wp_redirect/) version 5.1.0 as a way to specify the source of the redirect.

Use the browser console to check if the redirect HTTP response includes this header **to ensure the redirect was issued by WordPress** and not something else:

![X-Redirect-By HTTP response header in WordPress](https://wpelevator.com/wp-content/uploads/sites/12/2025/03/x-redirect-by-wordpress-header.png?strip=all&quality=90&resize=3120,1904)Alternatively, use this [curl](https://curl.se/docs/manpage.html) command to view the headers of any GET request:

```
curl --silent --output /dev/null --dump-header - https://wordpress.org/a
```

which returns:

```
HTTP/2 301 
server: nginx
date: Thu, 27 Mar 2025 09:22:14 GMT
content-type: text/html; charset=UTF-8
strict-transport-security: max-age=3600
x-olaf: ⛄
x-redirect-by: WordPress
location: https://wordpress.org/files/2022/10/a.png
x-frame-options: SAMEORIGIN
alt-svc: h3=":443"; ma=86400
x-nc: MISS ord 1
```

Note the `x-redirect-by: WordPress` header in the response.

A plugin or custom PHP code could be using the native `header( 'Location: ...' );` in which case it wouldn’t have the `X-Redirect-By` response header.

## Log WordPress Redirects

### Query Monitor X-QM-… Headers

Use the [Query Monitor plugin](https://wordpress.org/plugins/query-monitor/) to add a stack trace of all PHP functions leading up to the redirect in a `X-QM-Redirects-Redirect-Trace` header:

![WordPress redirect trace HTTP response header from Query Monitor](https://wpelevator.com/wp-content/uploads/sites/12/2025/03/wp-redirect-query-monitor-logs.png?strip=all&quality=90&resize=3120,1904)Note that this requires the user to be logged-in when making the requests, and have the necessary permissions to access to access the Query Monitor tooling.

### Use `x_redirect_by` Filter

Alternatively, you can build a custom solution that hooks into the [`x_redirect_by` filter](https://developer.wordpress.org/reference/hooks/x_redirect_by/) which fires right before the redirect header is sent, and log the stack trace to a file:

```
add_filter( 
	'x_redirect_by',
	function ( $x_redirect_by, int $status, string $location ) {
		// TODO: Log the value of debug_backtrace() to a file or DB.

		return $x_redirect_by;
	},
	100, 
	3 
);
```

Here is [a sample implementation](https://gist.github.com/kasparsd/f6b6e5572b5afeabe0d26f9120df26f0) as a must-use plugin that logs the redirects to a JSON file under the `wp-content/uploads` directory.