Reputation: 51
I have woocommerce page whose URL is like
https://www.example.com/brand/cocacola
I want to remove brand from URL and there will be no 404 error come
This link https://www.example.com/brand/cocacola
Need to convert into https://www.example.com/cocacola
But, the content of the page will be the same on archive page
Thanks in anticipation.
Upvotes: 2
Views: 320
Reputation: 51
I have got a solution to this problem.
**STEP (1)**
Add the below code in functions.php file
<!-- product_brand is your taxonomy name. Can be viewable from admin -->
add_filter( 'register_taxonomy_args', function( $args, $taxonomy ){
if( 'product_brand' === $taxonomy && is_array( $args ) ){
$args['rewrite']['slug'] = '/';
$args['rewrite']['with_front'] = false;
}
return $args;
}, 99, 2 );
**STEP (2)**
Save and refresh your permalink
** Code tested and its working fine.
Upvotes: 2
Reputation: 27
$str = "http://www.example.com/brand/cocacola";
$list = explode("/", $str);
echo $list[3];
echo ' or ';
$arr = explode('/'.$list[3], $str);
echo $arr[0] . $arr[1];
should print out: brand or http://www.example.com/cocacola
Upvotes: 1
Reputation: 421
One method of doing this is called masking with .htaccess and symlink
Here is a stackoverflow post that is relevant.
Use htaccess to mask folder name
Another option is to just use a wordpress filter
add_filter( 'register_taxonomy_args', function( $args, $taxonomy ){
if( 'brand' === $taxonomy && is_array( $args ) ){
$args['rewrite']['slug'] = '.';
$args['rewrite']['with_front'] = false;
}
return $args;
}, 99, 2 );
Then refresh your permalinks (code not tested, do enter into a live site.)
Upvotes: 0