1

I'm trying to use WP Rewrite to avoid having to enter anything into an htaccess file. So far, I have this one working just fine (it loads a post from a Custom Post Type):

$leader_structure = 'performance/leaders/%leader%';
$wp_rewrite->add_rewrite_tag("%leader%", '([^/]+)', "leader=");
$wp_rewrite->add_permastruct('leader', $leader_structure, false);

Basically, if you go to http://domain.com/performance/leaders/some-slug-here, it'll properly load the content from http://domain.com?leader=some-slug-here.

However, when I try it again with a non-custom-post-type (just normal WP posts), I can't get it to work. Here's what I'm doing:

$scenarios_structure = 'our-thinking/scenarios/%post%';
$wp_rewrite->add_rewrite_tag("%post%", '([^/]+)', "name=");
$wp_rewrite->add_permastruct('scenarios', $scenarios_structure, false);

So here, I want it so that http://domain.com/our-thinking/scenarios/some-post-slug should load the single WP post. But it doesn't. When I go to that URL, it redirects to http://domain.com/some-post-slug.

It's probably plainly obvious what I'm doing wrong to someone else, but I'm just not getting it. My WordPress permalink settings are set to "Post name". And my .htaccess file is untouched.

1

Your rewrite setting is good, but WordPress are redirect to the canonical url of the post. This is done by WodPress to prevent bad indexing for duplicate content (when an identical content is accessible via 2 different urls).

You can prevent that removing the canonical redirect filter:

add_action('wp_loaded', function() {
  remove_filter('template_redirect', 'redirect_canonical');
});

However this is not a recommned way, in fact, if you do it your post will be accessible both via the regular url and the new created url.

When you want to change the default post permastruct, the recommend way is to adjust the permalink structure in WordPress permalink settings, so in your case the permalink structure shoul be

our-thinking/scenarios/%postname%

and to prevent it affects any other custom post type or custom taxonomy, when you register them, use the argument $rewrite with the key 'with_front' set to false, e.g. :

...
'rewrite' => array( 'with_front' => false )
...
  • Thanks @G. M., that's making sense. So that will work for one permastruct. But what if I have others? Say our-thinking/interviews/%postname%, but you can only add one in the WP admin. – dallen Dec 4 '13 at 17:36
  • @dallen if you add more reqrite tags, it will be available for all the posts. So if you add 3 permailink structures, your post will be available via 4 urls.. this is a SEO hell. If I guess correctly you want setup url based on post category, if so, search this site, there are more than one Q for that already answered with working solution. – gmazzap Dec 4 '13 at 17:43
  • Doing this now: /our-thinking/%category%/%postname% and it's working. Which... is odd, because I'm 99.999% certain I did this before and it didn't work. – dallen Dec 4 '13 at 17:58

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.