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 | Show 3 more comments2 Answers
Reset to default 1Replace 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:
template_include
is a filter hook, not an action hook. It's fixed.- As pointed in the comments your rule
add_rewrite_rule('^ads.txt/', 'ads.txt', 'top');
is useless. It's fixed. - In this case,
redirect_canonical
should be used within a filter hook, not an action. - 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
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 ofindex.php
– Tom J Nowell ♦ Commented Sep 26, 2017 at 16:17