简体   繁体   中英

modifying woocommerce product URL in wordpress

I want to modify the current permalink structure which Woocommerce assigns to different products which we create. Right now, this is how the URL of a product looks like:

http://example.com/shop/coats-jackets/duis-aliquet-lorem-massa-1/

What I want to do is that, I want to modify the product URL so that it becomes:

http://example.com/coats-jackets/duis-aliquet-lorem-massa-1.html

I tried achieving the above URL structure by adding the following code inside the functions.php file but then whenever I tried to open a product, it gave a Page Not Found 404 error message:

add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3);

function wpse33551_post_type_link( $link, $post = 0 ){
    if ( $post->post_type == 'product' ){
        return home_url( '%product_cat%/' . $post->post_name . '.html' );
    } else {
        return $link;
    }
}

Basically I want to remove the store name from the URL, ie shop, and keep the category name followed by the product name and the product permalink suffix would be '.html'.

Looking forward to a solution. Thanks.

try adding the below code in you .htaccess file (Make a copy your old htaccess code)

# BEGIN REMOVE SLUG CUSTOM POST TYPE RULES
RewriteRule ^product /(.+)/$ /$1 [R=301,L]
# END REMOVE SLUG CUSTOM POST TYPE RULES

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