# Rewrite Rules Viewer and URL Tester

> Adds Tools > Rewrite Rules, listing every rewrite rule and showing which rule and query a URL path you enter would match, for debugging 404s and custom permalinks.

- Type: PHP
- Runs: Admin only
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: rewrite-rules, debugging, permalinks
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/dev-rewrite-rules-viewer/

## Code

```php
add_action( 'admin_menu', function () {
    add_management_page( 'Rewrite Rules', 'Rewrite Rules', 'manage_options', 'wpide-rewrite-rules', function () {
        $rules = (array) get_option( 'rewrite_rules' );
        $test  = isset( $_GET['wpide_path'] ) ? sanitize_text_field( wp_unslash( $_GET['wpide_path'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification -- read-only.
        $path  = trim( (string) wp_parse_url( $test, PHP_URL_PATH ), '/' );

        echo '<div class="wrap"><h1>Rewrite Rules</h1>';

        if ( ! get_option( 'permalink_structure' ) ) {
            echo '<div class="notice notice-warning"><p>Plain permalinks are active, so WordPress uses no rewrite rules.</p></div>';
        }

        echo '<form method="get"><input type="hidden" name="page" value="wpide-rewrite-rules"><label for="wpide-path"><strong>Test a path</strong></label> <input type="text" id="wpide-path" name="wpide_path" class="regular-text" value="' . esc_attr( $test ) . '" placeholder="/category/news/page/2/"> <button class="button">Test</button></form>';

        $matched = '';

        if ( '' !== $test ) {
            foreach ( $rules as $regex => $query ) {
                if ( preg_match( '#^' . str_replace( '#', '\#', $regex ) . '#', $path, $m ) || preg_match( '#^' . str_replace( '#', '\#', $regex ) . '#', urldecode( $path ), $m ) ) {
                    $matched  = $regex;
                    $resolved = preg_replace_callback( '/\$matches\[(\d+)\]/', function ( $x ) use ( $m ) {
                        return $m[ (int) $x[1] ] ?? '';
                    }, $query );
                    echo '<p>Matches <code>' . esc_html( $regex ) . '</code> → <code>' . esc_html( $resolved ) . '</code></p>';
                    break;
                }
            }

            if ( '' === $matched ) {
                echo '<p>No rule matches this path, so it would return a 404 unless a plugin handles it.</p>';
            }
        }

        echo '<p>' . esc_html( count( $rules ) ) . ' rules, in the order WordPress checks them.</p><table class="widefat striped"><thead><tr><th>Rule</th><th>Query</th></tr></thead><tbody>';

        foreach ( $rules as $regex => $query ) {
            printf( '<tr%1$s><td><code>%2$s</code></td><td><code>%3$s</code></td></tr>', $regex === $matched ? ' style="outline:2px solid #2271b1"' : '', esc_html( $regex ), esc_html( $query ) );
        }

        echo '</tbody></table></div>';
    } );
} );
```

## Notes

Read-only: it never changes rules. Enter a path such as /events/summer-fair/.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Rewrite Rules Viewer and URL Tester" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
