External/non-WP rewrite rule without QSA

I’m trying to add a new external/non-WP rewrite rule to my .htaccess but WP keeps adding [QSA,L] to the end of the rule.
How do I prevent the QSA part?

This line adds the RewriteRule:

Read More
$wp_rewrite->add_external_rule('go/([0-9]+)/?.*$', 'wp-content/plugins/xswpc/redirect.php?id=$1');

And this is the resulting line in my .htaccess

RewriteRule ^go/([0-9]+)/?.*$ /wp-content/plugins/xswpc/redirect.php?id=$1 [QSA,L]

What I’d like is:

RewriteRule ^go/([0-9]+)/?.*$ /wp-content/plugins/xswpc/redirect.php?id=$1 [L]

Related posts

2 comments

  1. The apache rewrite flags like QSA are hard coded, see source. Not sure why it would bother you, but if you really want to change it, then you can and should use the mod_rewrite_rules hook for that. Below an example on how to do it:

    add_filter( 'mod_rewrite_rules', 'wpse107528_change_mod_rewrite_rules' );
    function wpse107528_change_mod_rewrite_rules( $rules ) {
        $pattern = '/^(?<=RewriteRules^go.+$1s)([QSA,L])$/im';
        $replacement = '[L]';
        $rules = preg_replace( $pattern, $replacement, $rules );
        return $rules;
    }
    

    Beware I haven’t tested the Regex.

  2. Maybe you can try with add_rewrite_rule

    function wpse107528_add_external_rewrite_rule()
    {
        add_rewrite_rule(
            'go/([0-9]+)/?.*$',
            'wp-content/plugins/xswpc/redirect.php?id=$1',
            'top'
        );
        //Uncomment when testing to avoid already refresh permalink page in wp-admin
        //flush_rewrite_rules();
    }
    add_action( 'init', 'wpse107528_add_external_rewrite_rule' );
    

Comments are closed.