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.com/medical/website/nyc/ which only throws 404 error. However If you visit https://asif.bzstage.com/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

  • What's your custom post type slug? – filipecsweb May 25 '19 at 2:55
  • @filipecsweb location – Asifur Rahman May 27 '19 at 14:28

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.org/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.

  • Thank you so much! I've tried the same thing before expect using + in it. My custom post is little complicated. This refers to my another question - wordpress.stackexchange.com/questions/338623/… Both of the top slugs are actually taxonomies.However putting two %sector%/%service% breaks wordpress' normal permalinks. So I am now using sector/%service% while internally forwarding sector. If there is a better solution to my question(that I linked in this comment), could you please help me with that? – Asifur Rahman May 27 '19 at 20:16
  • I'll take a look at it. Cheers. – filipecsweb May 27 '19 at 20:17

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.