• 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/ → 404

    Resaving 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

    1. Activated WP myLinks → visited any two-segment URL → got 404
    2. Installed Rewrite Rules Inspector
    3. Tested /article/improve-interactions/ → saw the unscoped rule at the top
    4. Deactivated WP myLinks → all URLs worked again
    5. 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 );
    
    1. Replace 'your-hub-slug' with the slug of your hub page.
    2. Flush permalinks (Settings → Permalinks → Save).
    3. 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

    1. 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.
    2. Provide a “Link Base Slug” setting in the UI
      – Allow admins to change the base without custom code.
    3. 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!

    • This topic was modified 12 months ago by Alex De Py.
    • This topic was modified 12 months ago by Alex De Py.
    • This topic was modified 12 months ago by Alex De Py.
    • This topic was modified 12 months ago by Alex De Py.
    • This topic was modified 12 months ago by Alex De Py.
    • This topic was modified 12 months ago by Alex De Py.
Viewing 1 replies (of 1 total)
  • Thread Starter Alex De Py

    (@alexnleo)

    Additional Note:
    After adding the MU-plugin, the MyLink edit screen will display each link’s permalink as /your-hub-slug/<entry-slug>/. In reality, this is still the very same hub page—it’s simply now reachable via two URLs:

    • /your-hub-slug/
    • /your-hub-slug/<entry-slug>/

    Both addresses load identical content. If you prefer only the base URL, you can add a 301 redirect from /your-hub-slug/<entry-slug>/ back to /your-hub-slug/ (for example, via the Redirection plugin), or simply leave both working in parallel.

    • This reply was modified 12 months ago by Alex De Py.
Viewing 1 replies (of 1 total)

The topic ‘404 Errors Caused by Unscoped Rewrite Rule – Bugfix Report + Real Solutions’ is closed to new replies.