The wp rewrite rule I added to my plugin is not working. Here's my setup:


I'm trying to get the site's ads.txt to pull from my plugin's ads.txt. eg site.com/ads.txt pulls from wp-content/plugins/the-plugin/ads.txt - all done via serverside.

Plugin portion

add_action( 'init', 'wpse9870_init_external' );

function wpse9870_init_external()
    global $wp_rewrite;
    $plugin_url = plugins_url( 'ads.txt', __FILE__ );
    $plugin_url_sub = substr( $plugin_url, strlen( home_url() ) + 1 );
    // flush_rewrite_rules(true); // tried flushh ere
    $wp_rewrite->add_external_rule( '^ads.txt$', $plugin_url_sub ); // tried it with and without the caret (^)
    // flush_rewrite_rules(true); // also tried flush ere


WP_Rewrite Object {
    [non_wp_rules] => Array
        [^ads.txt$] => wp-content/plugins/the-plugin/ads.txt

Going to site.com/ads.txt results in a 404.

What else should I try?

  • did you flush rewrite rules? did you check your .htaccess file to see if anything was written there? – Milo Aug 31 '18 at 20:22
  • @Milo I just added flush_rewrite_rules(true); and refreshed the page a couple times with no success. I tried it above the add_external_rule and then tried it below. – Jacksonkr Aug 31 '18 at 20:38
  • External rules get added to your htaccess file, have you checked that file to see if it gets modified? – Milo Sep 1 '18 at 15:05
  • @Milo the htaccess is blank – Jacksonkr Sep 5 '18 at 16:31
  • @Milo It's because my htaccess was not writable by wordpress. If you put an answer I'll mark it. – Jacksonkr Sep 5 '18 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.