permalinks - help to remove last trailing slash using add_rewrite_rule

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

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

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

Share Improve this question edited Sep 26, 2017 at 16:18 Tom J Nowell 61.1k7 gold badges79 silver badges148 bronze badges asked Sep 26, 2017 at 14:44 Sidney OliveiraSidney Oliveira 11 bronze badge 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. – Sidney Oliveira 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. – Sidney Oliveira 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
 |  Show 3 more comments

2 Answers 2

Reset to default 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.
/* 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 );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742316577a4420955.html

相关推荐

  • permalinks - help to remove last trailing slash using add_rewrite_rule

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

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信