Htaccess redirect to stop access to parent pages but still allow child pages


I was using WordPress child page structure (for static/non-post pages) to create two separate page menu. It’s something like this:

Menu 1:

<?php wp_list_pages(‘child_of=1&sort_column=menu_order&title_li’); ?>

The post ID of the page with slug /pages/ is 1. Its daughter pages are /about/, /contact/ and /sitemap/

Menu 2:

<?php wp_list_pages(‘child_of=2&sort_column=menu_order&title_li’); ?>

The post ID of the page with slug /products/ is 2. Its daughter pages are /gadget1/, /gadget2/ and /gadget3/

Now, here is my problem. The parent pages /pages/ and /products/ are not supposed to be accessible. When the site visitor tries to access any of these pages, they are to be forwarded to an error page /404/. Hence, I implemented the following in the .htaccess file.

redirect 301 /products/

Hence, when the visitor accessed the /products/, they are prompty forwarded to the /404/ page as I intended. However, whenever I access any of the daughter pages of the /products/, I obtain a “redirect loop error”, something like that.

What I wanted to do is for the parent pages to redirect to /404/ but for the daughter pages to be normally accessible.

One solution I came up with is by using meta refresh and some PHP “if” function trick in the header instead of the .htaccess. However, this solution is not elegant for me. Depending on the speed of the connection, the browser will actually display the /pages/ and /products/ for some noticeable amount of time before redirecting to /404/ (the meta refresh is set to 0 seconds).

Thanks guys!

Try the following .htaccess:

RewriteMatch 301 /products/?$

That will only redirect of the request URL is /products or /producs/, so the parent pages, and will leave the daughter pages alone

Thanks ScallioXTX, but when I inserted it to .htaccess, I got Internal Server Error no matter what page I visit in the site.

Ehm, yes, it should be RedirectMatch, not RewriteMatch.
My bad.