在将类别基础设置为‘.’后,类别URL为404

时间:2019-12-16 作者:kevzettler

我想删除/category 来自URL。我已将类别基数设置为“”

我有一些“类别URL”,以前看起来像/category/category-name/sub-category-name 他们现在是/category-name/sub-category-name 这些URL以前呈现的页面将列出附加到该类别的所有帖子。他们现在返回404。

我可以在哪里调试这个?

4 个回复
SO网友:Heba Fareed

尝试flush permalinks 转到(设置>永久链接),然后单击“保存”,而不进行更改。

SO网友:Serkan Algur

有时你需要写新的动作。要解决类别库问题,需要添加和删除一些操作。您可以将此代码作为自定义插件进行尝试。也许这可以解决你的问题。代码完全删除/category/ 基础

将此代码添加到主题functions.php 或者使用此代码创建自定义插件。

/**
 * Add some actions to flush rewrite rules
 */

add_action(\'created_category\', \'flush_rewrite_rules_f_custom\');
add_action(\'delete_category\', \'flush_rewrite_rules_f_custom\');
add_action(\'edited_category\', \'flush_rewrite_rules_f_custom\');

// Init function

add_action(\'init\', \'remove_url_perma_cat\');

function flush_rewrite_rules_f_custom()
{
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

function remove_url_perma_cat()
{
    global $wp_rewrite;
    $wp_rewrite->extra_permastructs[\'category\'][0] = \'%category%\';
}

/**
 * Add some filter for redirect
 */

add_filter(\'category_rewrite_rules\', \'remo_rew_rul_c\');
add_filter(\'query_vars\', \'remo_q_vars_c\');
add_filter(\'request\', \'remo_crw_req\');

/**
 * Remove catergory base and redirect
 */
function remo_rew_rul_c($category_rewrite)
{
    global $wp_rewrite;

    $category_rewrite = array();

    $categories = get_categories(array( \'hide_empty\' => false ));

    foreach ($categories as $category) {
        $category_nicename = $category->slug;
        if ($category->parent == $category->cat_ID) {
            $category->parent = 0;
        } elseif (0 != $category->parent) {
            $category_nicename = get_category_parents($category->parent, false, \'/\', true) . $category_nicename;
        }
        $category_rewrite[ \'(\' . $category_nicename . \')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$\' ] = \'index.php?category_name=$matches[1]&feed=$matches[2]\';
        $category_rewrite[ \'(\' . $category_nicename . \')/page/?([0-9]{1,})/?$\' ] = \'index.php?category_name=$matches[1]&paged=$matches[2]\';
        $category_rewrite[ \'(\' . $category_nicename . \')/?$\' ] = \'index.php?category_name=$matches[1]\';
    }

    $old_category_base = get_option(\'category_base\') ? get_option(\'category_base\') : \'category\';
    $old_category_base = trim($old_category_base, \'/\');
    $category_rewrite[ $old_category_base . \'/(.*)$\' ] = \'index.php?crw=$matches[1]\';

    return $category_rewrite;
}

/**
 * New Query Vars : crw
 */
function remo_q_vars_c($public_query_vars)
{
    $public_query_vars[] = \'crw\';

    return $public_query_vars;
}

/**
 * Redirect based crw
 */
function remo_crw_req($query_vars)
{
    if (isset($query_vars[\'crw\'])) {
        $catlink = trailingslashit(get_option(\'home\')) . user_trailingslashit($query_vars[\'crw\'], \'category\');
        status_header(301);
        header("Location: $catlink");
        exit;
    }
}

SO网友:Tremi Dkhar

如果您使用的是Apache,请在.htaccess 在WordPress安装的根目录中。这将自动为您执行重定向。

注:更换http://www.example.com 使用您的网站URL。

RewriteRule ^category/(.+)$ http://www.example.com/$1 [R=301,L]

SO网友:Maciej Bis

是否尝试筛选$wp\\u查询对象?您可以使用此代码段直接在DB中搜索类别slug:

function pm_detect_term_slugs($query) {
    global $wpdb;

    // Do not run when Elementor is opened
    if((!empty($_REQUEST[\'action\']) && strpos($_REQUEST[\'action\'], \'elementor\') !== false) || isset($_REQUEST[\'elementor-preview\'])) {
        return $query;
    }

    // 1. Get the slug
    if(!empty($query[\'pagename\'])) {
        $slug = $query[\'pagename\'];
    } else if(!empty($query[\'name\'])) {
        $slug = $query[\'name\'];
    } else if(!empty($query[\'attachment\'])) {
        $slug = $query[\'attachment\'];
    }

    if(!empty($slug)) {
        $slug = basename($slug);

        // 2. Check if the slug is assigned to any post item
        $sql_query = "SELECT t.slug, t.term_id, tt.taxonomy FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON(tt.term_id = tt.term_id) WHERE 1=1 AND slug = %s";
        $term = $wpdb->get_row($wpdb->prepare($sql_query, array($slug)));

        // 3. Filter the query if any term was found
        if(!empty($term->taxonomy)) {
            $page = (!empty($query[\'paged\'])) ? $query[\'paged\'] : 1;

            $new_query = array(
                \'term\' => $term->slug,
                \'taxonomy\' => $term->taxonomy,
                \'paged\' => $page
            );

            // 4. Disable canonical redirect
            remove_action(\'template_redirect\', \'wp_old_slug_redirect\');
            remove_action(\'template_redirect\', \'redirect_canonical\');
            add_filter(\'wpml_is_redirected\', \'__return_false\', 99, 2);
            add_filter(\'pll_check_canonical_url\', \'__return_false\', 99, 2);

            return $new_query;
        }
    }
    return $query;
}
add_filter(\'request\', \'pm_detect_term_slugs\', 9999);
或者你可以使用我的插件Permalink Manager Pro 要筛选类别permalinks,请执行以下操作:https://permalinkmanager.pro/docs/basics/bulk-edit-wordpress-permalinks

相关推荐

在具有.htaccess密码的WordPress站点上运行安全扫描

首先,我不知道这个密码的正确术语是什么。我想这只是一句插话。htaccess,它可以防止机器人程序或未经授权访问临时环境,但也会不时破坏其他一些功能。密码可能不在。htaccess。我没有访问权限,也无法禁用它。我尝试了两个流行的安全插件来扫描我的网站(我可以说出他们的名字吗?),但他们会遇到以下错误:“扫描无法启动。这通常是因为站点无法发出出站请求或被阻止连接到自身。”“SiteCheck错误:无法正确扫描您的站点。401未经授权”是否有其他“工具”可以在我不禁用密码的情况下运行扫描?(我是否可以就Wo