I'm trying to rewrite this non existing url:
to this working wordpress url:
http://myshop.com/shop/category/medical
(but visitor must not see the full wordpress url)
. . .
And by other hand I need to rwwrite the url products from that category:
http://myshop.com/medical/my_friendly_unique_url_product
to:
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;
}
}
CodePudding user response:
Your apache rewrite rules do not perform rewrites like you have shown in your question. The http://myshop.com/medical/my_friendly_unique_url_product
request will be rewritten to http://myshop.com/shop/my_friendly_unique_url_product
, not the http://myshop.com/shop/category/medical/my_friendly_unique_url_product
.
To perform the same rewrites with the nginx, try this:
rewrite ^/medical(?:/category/?(.*)|/?)$ /shop/category/medical/$1;
rewrite ^/medical/(. ) /shop/$1;
To rewrite the http://myshop.com/medical/my_friendly_unique_url_product
request to the http://myshop.com/shop/category/medical/my_friendly_unique_url_product
, use this instead:
rewrite ^/medical(?:/category/?(.*)|/?)$ /shop/category/medical/$1;
rewrite ^/medical/(. ) /shop/category/medical/$1;
CodePudding user response:
I found solution using add_rewrite_rule wordpress function, put this code in functions.php theme or in a plugin:
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 );