$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugins - SEO Friendly URL on dynamic product page produced via shortcode|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin12PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far