如何修正带fbclid的PHP链接转WordPress的301重定向正则?
Absolutely, this is totally doable—your current setup just needs a small tweak to preserve the page slug and Facebook tracking parameters. Let's break down the issue and fix it:
The Problem with Your Current Setup
Your regex ^(.*)\.php(.*) correctly captures the parts before and after .php, but your target URL is hardcoded to the root domain. That's why all matching links are redirecting to the homepage instead of the specific post/page.
Correct Rank Math Redirect Configuration
Here's what you need to set up:
Source URL (Regex):
^(.*)\.php(\?.*)?$- Breakdown:
^(.*)\.php: Captures everything before the.phpextension (this is your page slug path, like/music/you_turn_me_on_again)(\?.*)?: Optionally captures the full query string (including?fbclid=...); the trailing?makes this part optional so it works for links without parameters too$: Ensures we match the end of the URL, avoiding accidental partial matches
- Breakdown:
Target URL:
$1/$2- Breakdown:
$1: Inserts the first captured group (your page's slug path)/: Replaces the.phpextension$2: Inserts the second captured group (the full query string with fbclid)
- Breakdown:
Testing the Fix
For your example link:http://www.beachwoodreporter.com/music/you_turn_me_on_again.php?fbclid=IwAR37SDAQdPrxMqwHQEY6dcs5rle1Mt0b0WubR9dL8WbaX3zoKNqjW0J84p0
This will now redirect correctly to:http://www.beachwoodreporter.com/music/you_turn_me_on_again/?fbclid=IwAR37SDAQdPrxMqwHQEY6dcs5rle1Mt0b0WubR9dL8WbaX3zoKNqjW0J84p0
Quick Notes
- Make sure you toggle on the "Regex" option for this redirect in Rank Math
- Test a mix of links (with and without fbclid parameters) to confirm everything works as expected
- This regex handles edge cases like
.phpfiles in nested subdirectories or links with multiple query parameters, since it captures the full query string
内容的提问来源于stack exchange,提问作者StephenOverflow

