0

I already tried in several ways to remove the last slash of the url that I created with add_rewrite_rule.

below is the implementation of my code

function master_load_ads_txt_template_include($template) {
  $is_load_ads_txt = (bool)get_query_var('ads-txt');
  if( $is_load_ads_txt ) {
    $template = get_template_part("template-parts/ads-txt");
  }

  return $template;
}
add_action( 'template_include', 'master_load_ads_txt_template_include' );

function master_load_ads_txt_rewrite() {
  add_rewrite_rule('ads.txt', 'index.php?ads-txt=true', 'top');
  add_rewrite_rule('^ads.txt/', 'ads.txt', 'top');
}
add_action('init', 'master_load_ads_txt_rewrite');

function master_load_ads_txt_query_vars( $query_vars ) {
  $query_vars[] = 'ads-txt';
  return $query_vars;
}
add_filter( 'query_vars', 'master_load_ads_txt_query_vars');

the code is working but does not remove the slash at the end of the url

enter image description here

8
  • Could be a plugin enforcing trailing slashes, or your .htaccess.
    – WebElaine
    Commented Sep 26, 2017 at 14:58
  • But I can not remove the endbar from all urls, only this. Commented Sep 26, 2017 at 16:12
  • 1
    Keep in mind that rewrite rules are not redirects, add_rewrite_rule('^ads.txt/', 'ads.txt', 'top'); won't work the way you think it will, the second parameter must always be some form of index.php
    – Tom J Nowell
    Commented Sep 26, 2017 at 16:17
  • Yes then I tried to remove the slash in the first add_rewrite_rule but it did not work. Commented Sep 26, 2017 at 16:19
  • 1
    Pretty sure WP core enforces the trailing slash in redirect_canonical, not possible to fix with just a rewrite rule.
    – Milo
    Commented Sep 26, 2017 at 21:50

2 Answers 2

1

Replace all your above code with the one below:

<?php

function master_load_ads_txt_template_include( $template ) {
    $is_load_ads_txt = (bool) get_query_var( 'ads-txt' );

    if ( $is_load_ads_txt ) {
        $template = locate_template( 'template-parts/ads-txt.php' );
    }

    return $template;
}

add_filter( 'template_include', 'master_load_ads_txt_template_include' );

function master_load_ads_txt_rewrite() {
    add_rewrite_rule( 'ads.txt', 'index.php?ads-txt=true', 'top' );

    // The line below doesn't work and it's useless.
    // add_rewrite_rule( '^ads.txt/', 'ads.txt', 'top' );
}

add_action( 'init', 'master_load_ads_txt_rewrite' );

function master_load_ads_txt_query_vars( $query_vars ) {
    $query_vars[] = 'ads-txt';

    return $query_vars;
}

add_filter( 'query_vars', 'master_load_ads_txt_query_vars' );

function redirect_canonical_callback( $redirect_url, $requested_url ) {
    $is_load_ads_txt = (bool) get_query_var( 'ads-txt' );

    if ( $is_load_ads_txt ) {
        return $requested_url;
    }

    return $redirect_url;
}

add_filter( 'redirect_canonical', 'redirect_canonical_callback', 100, 2 );

A few notes:

  1. template_include is a filter hook, not an action hook. It's fixed.
  2. As pointed in the comments your rule add_rewrite_rule('^ads.txt/', 'ads.txt', 'top'); is useless. It's fixed.
  3. In this case, redirect_canonical should be used within a filter hook, not an action.
  4. After putting the code above in your functions.php file don't forget to flush your permalinks by visiting Settings > Permalinks.
0
/* ORIGINAL CODE */
function master_load_ads_txt_template_include($template) {
    $is_load_ads_txt = (bool)get_query_var('ads-txt');
    if( $is_load_ads_txt ) {
        $template = get_template_part("template-parts/ads-txt");
    }
    return $template;
}
add_action( 'template_include', 'master_load_ads_txt_template_include' );

function master_load_ads_txt_rewrite() {
    /* UPDATE RULE */
    add_rewrite_rule('^ads.txt$', 'index.php?ads-txt=true', 'top');
}
add_action('init', 'master_load_ads_txt_rewrite');

function master_load_ads_txt_query_vars( $query_vars ) {
    $query_vars[] = 'ads-txt';
    return $query_vars;
}
add_filter( 'query_vars', 'master_load_ads_txt_query_vars');

/* ACTION TO ALLOW URL WITHOUT LAST TRAILING SLASH */
function disable_canonical_redirects_for_ads_txt( $redirect_url, $requested_url ) {
    if ( preg_match( '|ads\.txt|', $requested_url ) ) {
        return $requested_url;
    }
    return $redirect_url;
}
add_action( 'redirect_canonical', 'disable_canonical_redirects_for_ads_txt', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.