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! :(
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
.
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:31domain/p/
wouldn't be anything other than a page in your case, seems totally normal. – Milo Commented Dec 11, 2018 at 17:02