admin管理员组文章数量:1332359
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 );
本文标签: permalinkshelp to remove last trailing slash using addrewriterule
版权声明:本文标题:permalinks - help to remove last trailing slash using add_rewrite_rule 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742316576a2451935.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