admin管理员组文章数量:1130349
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 | Show 6 more comments1 Answer
Reset to default 0Your 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.
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
1 Answer
Reset to default 0Your 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.
本文标签: pluginsSEO Friendly URL on dynamic product page produced via shortcode
版权声明:本文标题:plugins - SEO Friendly URL on dynamic product page produced via shortcode 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749108333a2316951.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


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