Skip to content
Suyogya Shukla edited this page Feb 21, 2025 · 6 revisions

With the WPCOM Legacy Redirector plugin, site managers can create redirects for old, or legacy, URLs that now return 404 HTTP response status codes.

Redirect data can be stored in a CSV file OR in a post meta field. With the post meta field method, the resulting redirect is then attached to an existing post (and follows the post through future updates) whereas, with the CSV method, the redirect is attached to a hardcoded URL.

During plugin installation, be sure to download the plugin files from the develop branch

How do redirects work?

This plugin will not redirect valid URLs. If the URL does not return a 404, the redirect will not trigger, which means it won’t redirect live pages.

If a slug should be a 404 but matches the start of another slug, WordPress will return that instead and the redirect will not trigger.

All redirect bulk-loading needs to be done via WP-CLI commands.

If you have questions about how this plugin will work on your site or if you’d like to have us review your redirect strategy, please contact us before using this plugin.

How are redirects stored?

Redirects are stored as a custom post type and use the following fields:

  • post_name for the md5 hash of the "from" path or URL.
    • we use this column, since it's indexed and queries are super fast.
    • we also use an md5 just to simplify the storage.
  • post_title to store the non-md5 version of the "from" path.
  • one of either:
    • post_parent if we're redirect to a post; or
    • post_excerpt if we're redirecting to an alternate URL.

How are redirects created?

There are three ways to create redirects:

1. Adding redirects from WordPress Admin Visit the Redirect Manager → Add Redirect WP Admin screen and add redirects individually.

2. Adding redirects from a CSV file This method is recommended for bulk import redirects.

Create a .csv file and format the redirect URLs with one of the following mapping structures:

  • Relative Path: /redirect_from/,/redirect_to
  • Post ID: /redirect_from/,123(redirect_to_post_id)
  • URL: /redirect_from/,https://site.com(redirect_to_url)

All of the methods above require that redirect_from values contain a beginning slash (/) and trailing slash (/).

Follow these instructions to add the redirect: https://github.com/Automattic/WPCOM-Legacy-Redirector/wiki/Adding-Redirects-from-a-CSV-File

3. Adding Redirects from Post Meta Fields This method creates a redirect from the address found in the meta field to the post that has the meta field.

Follow these instructions to add the redirect: https://github.com/Automattic/WPCOM-Legacy-Redirector/wiki/Adding-Redirects-from-Post-Meta-Fields

How to identify a legacy redirect configured through this plugin?

To identify a legacy redirect configured with the WPCOM-Legacy-Redirector plugin, initiate a CURL request for the from address of the redirect and look for a x-legacy-redirect: HIT in response headers as the plugin is configured to add this header to any URLs being redirected by it: https://github.com/Automattic/WPCOM-Legacy-Redirector/blob/32fc3ebae7d76bba6287cd21de67dfc71e705a54/includes/class-wpcom-legacy-redirector.php#L90

How to delete a redirect?

To delete a redirect, we will first need the post ID in which the redirect is stored. This can be obtained by checking for the from address in the database, stored in the post_title field. For example, if the from address of a redirect is automattic.com/work the following SQL query should return the relevant post ID. For this example, the query is being run against a WPVIP environment using the wp db query command:

"SELECT * FROM wp_posts WHERE post_type = 'vip-legacy-redirect' AND post_title = 'automattic.com/work';"

Once you have the post ID, deleting it through WP-CLI should suffice. For this example, we'll consider that the post ID for this redirect was 99. To delete it, we'll need to run the following WP-CLI command:

wp post delete 99 --post_type="vip-legacy-redirect"