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! :(

  • 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
    Dec 11 '18 at 14:31
  • Pretty Permalinks definitely need to be enabled for rewrite rules to work. Did you flush rewrites after adding your rule?
    – Milo
    Dec 11 '18 at 16:19
  • I did yes. Everytime I've changed them I've clicked 'save permalinks'.
    – Alan
    Dec 11 '18 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.com/a/rSyJO4C
    – Alan
    Dec 11 '18 at 16:40
  • Nothing wrong there, visiting domain.com/p/ wouldn't be anything other than a page in your case, seems totally normal.
    – Milo
    Dec 11 '18 at 17:02

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_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.

  • I had registered the query variable using add_filter('query_vars' previously. I've amended the code to what you've provided and still no cigar. I had to alter it slightly because the id is alphanumeric and can contain underscores. Also some text can follow it, ie the product description so doesn't it need to be like this? '^detail/(d+)/.*?$', 'index.php?name=detail&id=$matches[1]' thanks for your help mate
    – Alan
    Dec 11 '18 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.