简体   繁体   中英

How to setup Rewrite rule in WordPress

How can I setup a rewrite rule in WordPress with the following URL "https://mywebsite.com/product-detail/model=MH-TOOL-152&isToolId=1" This is the redirected link after scanning a QR Code. I should get the parameter model value because this is the basis of the product SKU and will place my shortcode under the product-detail page.

I've tried this one below, which redirected me to the home page. I'm trying to setup that it will just display it on the product detail page.

function mywebsite_rewrite_rule() {
   add_rewrite_rule('^product-detail/([^/]*)/?', 'index.php/model=$matches[1]', 'top');
}
add_action('init', 'mywebsite_rewrite_rule', 10, 0);

I have already figured out how to use the rewrite rule in WP. This is what I've been working on.

// to get the model variable
add_filter('query_vars', 'add_model_var_prodDetail', 10, 1);
function add_model_var_prodDetail($vars_sku){
    $vars_sku[] = 'model';
    return $vars_sku;
}

//rewrite the URL on the specific page product-detail
add_action('init', 'rewrite_rule_productDetail', 10, 0);
function rewrite_rule_productDetail() {
    add_rewrite_rule('^product-detail/([^/]*)/?','index.php?post_type=page&pagename=product-detail&model=$matches[1]','top');
    add_rewrite_tag( '%model%', '(.+)' );
}

Then retrieve the model using get_query_var

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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