add_rewrite_rule() to route to file other than index.php

add_rewrite_rule(
    '^invite/([^/]+)',
    'index.php?pagename=invite&iid=$matches[1]',
    'top'
);

The “internal” rewrite rule above will route traffic through index.php using the pagename argument (which necessitates having a page with slug invite).

I want to direct routing to invite.php (which will render an ICS calendar file and force download).

I looked into the below implementation, however, this appears to directly modify .htaccess file. My site is using nginx so this won’t work.

add_rewrite_rule(
    '^invite/([^/]+)',
    'invite.php?iid=$matches[1]',
    'top'
);

1 Answer
1

add_action('parse_query', function(&$wp_query) {

    if (array_key_exists('iid', $wp_query->query_vars) && $wp_query->query['pagename'] === 'invite') {

        $iid = get_query_var('iid');

        // Do some stuff

        exit();

    }

    return;

});

Leave a Comment