WordPress.org

Ready to get started?Download WordPress

Forums

Prevent direct access to child category (2 posts)

  1. trmash
    Member
    Posted 1 year ago #

    Hi,

    I have a website that is essentially an online newspaper/magazine, where there are categories and within them child categories.

    I've realised that visitors can access the child categories without going via the parents, and I'm wondering if there's any way to prevent this as it alters the look of the posts.

    For example, domain.com/entertainment/art is the preferred URL, but the same thing can also be accessed via domain.com/art and the styling is affected.

    Is there anything I can do (preferably a future-proof overall method rather than category-specific codes) to redirect the readers to the correct path?

    I've tried if ( $post->post_parent == 'entertainment' ) {$entertainment == get_the_category(); header( 'Location: ..\entertainment/'.$entertainment);} but it causes redirect loops - and I have a few other parent and child categories that need addressing too.

    Is there possibly even a .htaccess solution? I tried a 301 without luck, but I'm no expert at either PHP or altering .htaccess files.

    Any suggestions much appreciated.

  2. Pioneer Valley Web Design
    Member
    Posted 1 year ago #

    any way to prevent this as it alters the look of the posts

    Why would that not be what to fix?

Topic Closed

This topic has been closed to new replies.

About this Topic