Blog installed in subdirectory but need to create pages in root. How to use permalinks?

Our companies blog is installed in /blog. Our permalink structure is /%postname%/ so posts currently look like https://www.domain.com/blog/post

We’ve been asked to publish a series of guides which must be installed in /buying-guides/ e.g. https://www.domain.com/buying-guides/kittens i.e. outside of the WordPress folder.

Read More

I thought the answer was to simply create a page called buying-guides and then create a page per guide as children. We then end up with URLs such as https://www.domain.com/blog/buying-guides/kittens. I then created an .htaccess rule in our root folder with

RewriteRule ^buying-guides/(.+)$    /blog/$1 [L,NC]

and it seems to be working beautifully! Until I realized that, there is more to it as WordPress is obviously not aware of the rewrite. For example, we use the_permalink() function to generate the link in our page titles and this now obviously generates the incorrect link (although it would still work, but we don’t want customers too see any mention of /blog/ when browsing the buying guides.

Is there a good solution to the above issue? Perhaps a filter than automatically changes all URLs just before the page is rendered from /blog/buying-guides/ to /buying-guides/?

Any help would be greatly appreciated!

Related posts

1 comment

  1. Have you tried to use the_permalink() filter hook?

    add_filter('the_permalink', 'remove_blog');
    function remove_blog($url) {
        //only affects the permalink of pages
        if(is_page()){
            return str_replace('blog/', '', $url);
        }
        return $url;
    }
    

    In combination with your current rewrite rule in the .htaccess of your root directory.

Comments are closed.