使用ADD_REWRITE_RULE帮助删除最后一个尾随斜杠

时间:2017-09-26 作者:Sidney Oliveira

我已经尝试了几种方法来删除我创建的url的最后一个斜杠add_rewrite_rule.

下面是我的代码的实现

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\');
代码正在运行,但没有删除url末尾的斜杠

enter image description here

2 个回复
SO网友:filipecsweb

将上述所有代码替换为以下代码:

<?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 是过滤器挂钩,而不是动作挂钩。它是固定的add_rewrite_rule(\'^ads.txt/\', \'ads.txt\', \'top\'); 是无用的。它是固定的redirect_canonical 应在过滤器挂钩中使用,而不是在操作中使用functions.php 文件不要忘记通过访问Settings > Permalinks.

SO网友:Elton Peetz
/* 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 );
结束

相关推荐

Change Taxonomy Permalinks

我有自定义帖子,我创建了一个显示所有自定义帖子的页面。示例:www.example.com/archive-page我想知道是否可以更改与此自定义帖子相关的类别和标签的永久链接。现在我有:www.example.com/my-custom-post-type-cats/my-category-1www.example.com/my-custom-post-type-tags/my-tag-1</我想要这样的东西:www.example.com/archive-page?category=1www.e