404 Errors Caused by Unscoped Rewrite Rule – Bugfix Report + Real Solutions
-
Hi there,
I’d like to share a bugfix report for WP myLinks—this is more than just a bug report; it includes root-cause analysis, how I diagnosed it, a working patch, and recommendations for both users and the plugin author.
1. Environment
- WordPress: 6.8.1
- Theme: Custom child theme (
patch-child-master) - Permalink structure:
/post-name/
2. The Problem
As soon as WP myLinks is activated (even after flushing permalinks via Settings → Permalinks → Save), any two-segment URL on the site returns a 404. Examples:
/article/example-post/ → 404
/product/sample-product/ → 404
/category/news/ → 404
/parent/child-page/ → 404
/custom-cpt/some-slug/ → 404Resaving permalinks or clearing cache does not fix it.
3. Root Cause Analysis
Using Rewrite Rules Inspector, I discovered that WP myLinks registers an unscoped rewrite rule:
[^/]+/([^/]+)/?$ → index.php?attachment=$matches[1] (source: mylink)Because this regex:
- Is extremely broad (matches any two-segment path)
- Is injected at the top of the rewrite stack
WordPress never reaches more specific rules (
page,single,product, or custom post-type rules), so everything 404s.4. How I Diagnosed It
- Activated WP myLinks → visited any two-segment URL → got 404
- Installed Rewrite Rules Inspector
- Tested
/article/improve-interactions/→ saw the unscoped rule at the top - Deactivated WP myLinks → all URLs worked again
- Confirmed it was not just a missing permalinks flush
5. Immediate Fix (for Users)
Place this small plugin in
wp-content/mu-plugins/mylink-rewrite-fix.php, to scope the CPT slug and eliminate the conflict:<?php /** * MU Plugin: Scope WP myLinks CPT under your hub page slug */ add_filter( 'register_post_type_args', function( $args, $post_type ) { if ( $post_type === 'mylink' ) { $args['rewrite'] = [ 'slug' => 'your-hub-slug', // e.g. 'links', 'resources', etc. 'with_front' => false, 'feeds' => false, 'pages' => false, ]; } return $args; }, 10, 2 );- Replace
'your-hub-slug'with the slug of your hub page. - Flush permalinks (Settings → Permalinks → Save).
- Clear any caches.
Result:
- All WP myLinks URLs live under
/your-hub-slug/.../ - All other two-segment URLs resolve normally
6. Recommendations for the Plugin Author
- Use the user’s hub-page slug as the rewrite base
– If the plugin lets the user designate a “MyLinks” page, automatically read its slug and apply it to the CPT rewrite. - Provide a “Link Base Slug” setting in the UI
– Allow admins to change the base without custom code. - Avoid unscoped root-level regex
– Only generate rewrite rules under that base slug; never use a catch-all two-segment pattern.
Implementing these changes would prevent this critical 404 issue on sites with any other two-level URLs.
Thanks for your work on WP myLinks—this plugin has great potential for managing a link hub, and I hope this report and patch help make it rock-solid. Let me know if you need any further details or testing!
The topic ‘404 Errors Caused by Unscoped Rewrite Rule – Bugfix Report + Real Solutions’ is closed to new replies.