0

So I have a situation on a site where I need multiple permalink structures. Now, I already know what you're going to say because literally every Google and SX result I've looked through says the same thing: Don't do this; this is a bad idea; here be dragons; etc. I've already heard all that. I've already sorted out link canonicalization and the og:url and SEO isn't an issue.

The reality is, though, the client wants a variation of the existing site in a "subdirectory" that is the same page structure, but has some PHP logic within template files. So, it's the same content, but the page looks a little different according to business rules for the subset of users that will be accessing via this different URL.

So with that understanding, what is the best way to define in Wordpress that:

http://www.example.com/abc/ should be rewritten to http://www.example.com/

http://www.example.com/abc/about/ should be rewritten to http://www.example.com/about/

and so on?

I currently have rules defined manually using add_rewrite_rule():

add_rewrite_rule('abc/?$','index.php?page_id=6','top');
add_rewrite_rule('abc/about/?$','index.php?page_id=9','top');

But that's unmaintainable. I'd like a way for it to act like those add_rewrite_rule() calls, but apply to the whole site, ideally without having to go through and look up every URL individually from the database.

Rewrites only, redirects are not acceptable.

Thoughts?

1

I think I found a solution to this with a function that's not commonly used: add_permastruct().

This does the trick of what I described above:

add_rewrite_tag('%page%','([^/]+)', 'pagename=');
add_permastruct('abc','/abc/%page%/',false);

add_rewrite_rule('abc/?$','index.php?page_id=6','top');

The add_rewrite_tag() defines the pagename value that I want Wordpress to lookup. Then add_permastruct() defines my custom structure with that value.

The last rule is just to handle the main page manually.

I believe this would only work with one-level of hierarchy, but I may come back and tweak the regex later to improve it. For now, this works for my purposes.

0

What you want and what you need is two sites with content duplication, probably under a multisite install. While you still need to write some duplication code it is a much easier to develop and debug and is low on dragons.

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.