定制 automattic/wpcom-legacy-redirector 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

automattic/wpcom-legacy-redirector

Composer 安装命令:

composer require automattic/wpcom-legacy-redirector

包简介

WordPress plugin for handling large volumes of legacy redirects in a scalable manner.

README 文档

README

Stable tag: 2.0.0-alpha Requires at least: 6.4 Tested up to: 6.7 Requires PHP: 8.2 License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html Tags: redirects, 301, legacy, migration, seo Contributors: automattic, WordPress VIP

A WordPress plugin for handling legacy redirects in a scalable manner. Designed for high-traffic sites with large volumes of redirects.

At a Glance

  • Scalable: Handles thousands of redirects efficiently using MD5-indexed lookups
  • Admin UI: Add and manage redirects through the WordPress admin
  • WP-CLI support: Bulk import/export via command line
  • Multisite compatible: Works on single sites and multisite networks
  • Query parameter preservation: Optionally preserve UTM and other tracking parameters
  • VIP-ready: Built for WordPress VIP environments

Installation

  1. Upload the plugin folder to /wp-content/plugins/ or install via the WordPress admin
  2. Activate the plugin through the 'Plugins' menu in WordPress
  3. Navigate to Tools > Redirects to add or manage redirects

Usage

Adding Redirects via Admin

  1. Go to Tools > Redirects > Add Redirect
  2. Enter the "Redirect From" path (e.g., /old-page)
  3. Enter the "Redirect To" destination (URL or post ID)
  4. Click "Add Redirect"

Adding Redirects via WP-CLI

# Add a single redirect
wp wpcom-legacy-redirector insert-redirect /old-page https://example.com/new-page

# Redirect to an internal post by ID
wp wpcom-legacy-redirector insert-redirect /old-page 123

# Import redirects from CSV
wp wpcom-legacy-redirector import-from-csv /path/to/redirects.csv

# Export redirects to CSV
wp wpcom-legacy-redirector export-to-csv /path/to/export.csv

Programmatic Usage

use Automattic\LegacyRedirector\Domain\Destination;
use Automattic\LegacyRedirector\Domain\SourceUrl;
use function Automattic\LegacyRedirector\container;

// Get the redirect manager via the helper function (recommended for third-party code)
$manager = container()->manager();

// Add a redirect to an external URL
$source      = SourceUrl::from_string( '/old-page' );
$destination = Destination::from_mixed( 'https://example.com/new-page' );
$result      = $manager->create_redirect( $source, $destination );

if ( $result->is_error() ) {
    // Handle error: $result->error_code(), $result->error_message()
}
$redirect_id = $result->redirect_id();

// Add a redirect to an internal post by ID
$source      = SourceUrl::from_string( '/another-old-page' );
$destination = Destination::from_mixed( $post_id );
$result      = $manager->create_redirect( $source, $destination );

// Check if a redirect exists and get its data
$redirect_data = container()->executor()->get_redirect_data( '/old-page' );
if ( $redirect_data ) {
    $redirect_url    = $redirect_data['url'];
    $redirect_status = $redirect_data['status_code'];
}

Multisite Support

The plugin works on WordPress multisite installations:

  • Per-site redirects: Each site manages its own redirects independently
  • No cross-site leakage: Redirects on Site A do not affect Site B
  • WP-CLI support: Use --url=site.example.com to manage specific sites

WP-CLI Multisite Examples

# Add redirect on specific site
wp wpcom-legacy-redirector insert-redirect /old /new --url=site2.example.com

# Export redirects from specific site
wp wpcom-legacy-redirector export-to-csv /path/to/export.csv --url=site2.example.com

How It Works

Redirects are stored as a custom post type (vip-legacy-redirect) with:

  • MD5 hash of the source URL in post_name (indexed for fast lookups)
  • Original URL in post_title (human-readable)
  • Destination as either post_parent (internal) or post_excerpt (external)

The plugin intercepts 404 requests early (priority 0 on template_redirect) and performs a redirect if a match is found.

Hooks and Filters

Preserve Query Parameters

By default, query parameters are stripped during redirect lookup. To preserve specific parameters (like UTM codes):

add_filter( 'wpcom_legacy_redirector_preserve_query_params', function( $params, $url ) {
    return array( 'utm_source', 'utm_medium', 'utm_campaign' );
}, 10, 2 );

Modify Redirect Status Code

Change the HTTP status code (default: 301):

add_filter( 'wpcom_legacy_redirector_redirect_status', function( $status, $url ) {
    return 302; // Temporary redirect
}, 10, 2 );

Modify Request Path

Alter the path before redirect lookup:

add_filter( 'wpcom_legacy_redirector_request_path', function( $path ) {
    return strtolower( $path ); // Case-insensitive matching
} );

WP-CLI Commands

Command Description
insert-redirect Add a single redirect
import-from-csv Bulk import from CSV file
import-from-meta Import from post meta
export-to-csv Export all redirects to CSV
find-domains List destination domains

For detailed command options, run wp help wpcom-legacy-redirector.

Documentation

See the Wiki for detailed documentation.

Support

Please use GitHub Issues only for bug reports and feature requests, not general support questions.

Contributing

We welcome contributions! See CONTRIBUTING.md for guidelines.

Changelog

See CHANGELOG.md for the full list of changes.

License

Licensed under GPL-2.0-or-later. See LICENSE for details.

automattic/wpcom-legacy-redirector 适用场景与选型建议

automattic/wpcom-legacy-redirector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 155.8k 次下载、GitHub Stars 达 26, 最近一次更新时间为 2020 年 09 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 automattic/wpcom-legacy-redirector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 automattic/wpcom-legacy-redirector 我们能提供哪些服务?
定制开发 / 二次开发

基于 automattic/wpcom-legacy-redirector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 155.8k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 26
  • 点击次数: 17
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 26
  • Watchers: 71
  • Forks: 16
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2020-09-29