permalinks - Internal forward link with add_rewrite_rule

I want to forward a link to another link but internally. I've set up the wordpress to show post links like this -

I want to forward a link to another link but internally. I've set up the wordpress to show post links like this - / which only throws 404 error. However If you visit / (location instead of medical), it works. So I want to forward any url containing medical to location.

Thanks in advance for your help

I want to forward a link to another link but internally. I've set up the wordpress to show post links like this - https://asif.bzstage/medical/website/nyc/ which only throws 404 error. However If you visit https://asif.bzstage/location/website/nyc/ (location instead of medical), it works. So I want to forward any url containing medical to location.

Thanks in advance for your help

Share Improve this question asked May 24, 2019 at 18:52 Asifur RahmanAsifur Rahman 52 bronze badges 2
  • What's your custom post type slug? – filipecsweb Commented May 25, 2019 at 2:55
  • @filipecsweb location – Asifur Rahman Commented May 27, 2019 at 14:28
Add a comment  | 

1 Answer 1

Reset to default 0

The code below should work for you as long as:

  1. Your CPT slug is location.
  2. You don't have a custom query_var registered for your CPT location. You probably don't have. Refer to https://codex.wordpress/Function_Reference/register_post_type#query_var for more information, it's very usefull, btw.

And I'm assuming the string nyc is the slug of your single custom post.

function rewrite_medical_url() {
    add_rewrite_rule( '^medical/[^/]+/([^/]+)/?', 'index.php?location=$matches[1]', 'top' );
}

add_action( 'init', 'rewrite_medical_url' );

Put the code in your functions.php file.

In order for the rewrite rule start taking effect you should visit Settings > Permalinks to flush your permalinks.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745459331a4628634.html

相关推荐

  • permalinks - Internal forward link with add_rewrite_rule

    I want to forward a link to another link but internally. I've set up the wordpress to show post links like this -

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信