plugins - SEO Friendly URL on dynamic product page produced via shortcode

admin2025-06-05  2

I have created a product page in wordpress, /product.

This currently takes the id of a product /product/?id=123

The page has a shortcode that then fetches and displays the relevent data

e.g. [product_shortcode]

I'm wondering how I can make this URL more SEO friendly. I've tried using a URL Rewrite via .htaccess so that I could link to the pages like so; /product/id/product-description

However Wordpress throws a 404 even though I can see some of my shortcode runs (the title tag changes)

Can anyone advise how I can achieve what I'm trying to do here? Been at it for 2 weeks! :(

I have created a product page in wordpress, /product.

This currently takes the id of a product /product/?id=123

The page has a shortcode that then fetches and displays the relevent data

e.g. [product_shortcode]

I'm wondering how I can make this URL more SEO friendly. I've tried using a URL Rewrite via .htaccess so that I could link to the pages like so; /product/id/product-description

However Wordpress throws a 404 even though I can see some of my shortcode runs (the title tag changes)

Can anyone advise how I can achieve what I'm trying to do here? Been at it for 2 weeks! :(

Share Improve this question edited Dec 11, 2018 at 10:19 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Dec 11, 2018 at 9:51 AlanAlan 113 bronze badges 11
  • OK so I've worked out what the true unfriendly URL is by disabling permalinks. I now have this rule: add_rewrite_rule('^product\/(\d+)\/.*','index.php?page_id=5&id=$matches[1]','top'); Which redirects me to the homepage. Why? Also, if I re-enable permalinks it 404's. Can you not have custom rewrites with permalinks enabled? I'm very confused. – Alan Commented Dec 11, 2018 at 14:31
  • Pretty Permalinks definitely need to be enabled for rewrite rules to work. Did you flush rewrites after adding your rule? – Milo Commented Dec 11, 2018 at 16:19
  • I did yes. Everytime I've changed them I've clicked 'save permalinks'. – Alan Commented Dec 11, 2018 at 16:28
  • I've installed a 'Rewrite analyzer' plugin, which doesn't seem to match the condition. Though having said that nothing seems to match which seems odd. Screenshot here: imgur/a/rSyJO4C – Alan Commented Dec 11, 2018 at 16:40
  • Nothing wrong there, visiting domain/p/ wouldn't be anything other than a page in your case, seems totally normal. – Milo Commented Dec 11, 2018 at 17:02
 |  Show 6 more comments

1 Answer 1

Reset to default 0

Your rule needs a little tweak. You also need to add id to valid query vars.

function wpd_test_rule() {
    add_rewrite_tag( '%id%', '([0-9]+)' );
    add_rewrite_rule(
        '^product/([0-9]+)/([^/]+)/?$',
        'index.php?page_id=5&id=$matches[1]',
        'top'
    );
}
add_action( 'init', 'wpd_test_rule' );

Note that WordPress doesn't put query vars in $_GET, you'll need to adjust your code to use get_query_var('id') to fetch the value. I'd also consider using something more unique than id.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749108380a316430.html

最新回复(0)