【问题标题】:NginX 1.16 - URL redirections for one wordpress category and all its productsNginX 1.16 - 一个 wordpress 类别及其所有产品的 URL 重定向
【发布时间】:2022-02-20 23:31:14
【问题描述】:

我正在尝试重写这个不存在的网址:

http://myshop.com/medical

到这个工作的 wordpress 网址:

http://myshop.com/shop/category/medical

(但访问者不能看到完整的 wordpress url)

。 . .

另一方面,我需要 rwwrite 来自该类别的 url 产品:

http://myshop.com/medical/my_friendly_unique_url_product

到:

http://myshop.com/shop/category/medical/my_friendly_unique_url_product

server {
    root /var/www/myshop.com;
    server_name myshop.com www.myshop.com;
    include global/global.conf;
    include global/wordpress.conf;
    location /medicals {
        # http://myshop.com/medicals
        # to
        # http://myshop.com/shop/category/medicals/
        rewrite ^/medicals$ /shop/category/medicals break;

        # http://myshop.com/medicals/dinamic_unique_url_product
        # to
        # http://myshop.com/shop/category/medicals/dinamic_unique_url_product        
        rewrite ^/medicals/(.*)$ /shop/category/medicals/$1 break;
        
    }
}

【问题讨论】:

    标签: wordpress nginx url redirect


    【解决方案1】:

    您的 apache 重写规则不会像您在问题中显示的那样执行重写。 http://myshop.com/medical/my_friendly_unique_url_product 请求将被重写为http://myshop.com/shop/my_friendly_unique_url_product,而不是http://myshop.com/shop/category/medical/my_friendly_unique_url_product。 要使用 nginx 执行相同的重写,请尝试以下操作:

    rewrite ^/medical(?:/category/?(.*)|/?)$ /shop/category/medical/$1;
    rewrite ^/medical/(.+) /shop/$1;
    

    要将http://myshop.com/medical/my_friendly_unique_url_product 请求重写为http://myshop.com/shop/category/medical/my_friendly_unique_url_product,请改用:

    rewrite ^/medical(?:/category/?(.*)|/?)$ /shop/category/medical/$1;
    rewrite ^/medical/(.+) /shop/category/medical/$1;
    

    【讨论】:

    • 我正在尝试您的解决方案,但我无法成功
    • 1.此规则将无法放入 location 块中,它们应放置在 server 上下文中。 2. 如果您通过fastcgi_pass 指令调用PHP,此规则将不起作用。这是因为 WordPress 依赖于 REQUEST_URI FastCGI 变量,该变量不会被 rewrite 指令更改(它是一个 $uri )。您需要在include fastcgi_params; 指令之后重新定义该变量,例如fastcgi_param REQUEST_URI $uri$is_args$args;.
    【解决方案2】:

    我找到了使用 add_rewrite_rule wordpress 函数的解决方案,将此代码放在 functions.php 主题或插件中:

    add_action( 'init',  function() {
        flush_rewrite_rules();    
        add_rewrite_rule( '^medicals/page/([0-9]{1,})/?', 'index.php?product_cat=medicals&paged=$matches[1]', 'top' );
        add_rewrite_rule( '^medicals/(.*)/?', 'index.php?product=$matches[1]', 'top' );
        add_rewrite_rule( 'medicals/?$', 'index.php?product_cat=medicals', 'top' );
    } );
    
    function prefix_filter_news_permalink( $url, $post ) 
    { 
        $terms = get_the_terms( $post->ID, 'product_cat' );
        $nterms = get_the_terms( $post->ID, 'product_tag'  );
        foreach ($terms as $prod_term) {
            $product_cat_id = $prod_term->term_id;
            if ( $product_cat_id== 540 ) { //my medicals ID category
                return trailingslashit( home_url('/medicals/'. $post->post_name ) );
                break;
            } else {
                $product_parent_categories_all_hierachy = get_ancestors( $product_cat_id, 'product_cat' );  
                foreach($product_parent_categories_all_hierachy as $last_parent_cat_value){
                    if ($last_parent_cat_value == 540 ) { //my medicals ID category
                        return trailingslashit( home_url('/medicals/'. $post->post_name ) );
                        break;
                    }
                }
            }
        }   
        return $url;
    }
    add_filter( 'post_type_link', 'prefix_filter_news_permalink', 10, 2 );
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2016-09-02
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多