简体   繁体   English

使用rediect 301从URL删除文件夹

[英]Use rediect 301 to remove folder from URL

I have a web site on woocommerce with Sahifa theme, I don't why they're a lot of link with 404 error, like those: 我有一个以Sahifa为主题的woocommerce网站,我不为什么它们与404错误的链接很多,例如:

/categoria-prodotto/aromi/ feed /page/10/?orderby=date / categoria-prodotto / aromi / feed / page / 10 /?orderby = date

/categoria-prodotto/accessori/ feed /page/2/ / categoria-prodotto / accessori / feed / page / 2 /

I want remove /feed/ folder (I don't use feed) and reciderct on the same URL without /feed/ folder. 我想删除/ feed /文件夹(我不使用feed),并在没有/ feed /文件夹的同一URL上重新注册。

For example: 例如:

/categoria-prodotto/accessori/feed/page/2/ / categoria-prodotto / accessori / feed / page / 2 /

To

/categoria-prodotto/accessori/page/2/ / categoria-prodotto / accessori / page / 2 /

You can try this code by pasting it in your functions.php file 您可以通过将其粘贴到functions.php文件中来尝试该代码

function na_remove_slug( $post_link, $post, $leavename ) {

    if ( 'feed' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }

    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

    return $post_link;
}
add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );

function na_parse_request( $query ) {

    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'feed', 'page' ) );
    }
}
add_action( 'pre_get_posts', 'na_parse_request' );

After paste this code, You need to update your permalinks. 粘贴此代码后,您需要更新永久链接。 This function is basically remove that slug from all of your urls 此功能基本上是从所有网址中删除该段代码

Thanks for answer, but i'd like remove /feed/ and put redirect 301 on link without /feed/ 感谢您的回答,但我想删除/ feed /并将重定向301放在没有/ feed /的链接上

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM