PDA

View Full Version : Htaccess redirect to stop access to parent pages but still allow child pages



gareth_esutera
04-30-2010, 03:17 PM
Hi!

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'); ?>

http://www.domain.com/pages/about/
http://www.domain.com/pages/contact/
http://www.domain.com/pages/sitemap/

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'); ?>

http://www.domain.com/products/gadget1/
http://www.domain.com/products/gadget2/
http://www.domain.com/products/gadget3/

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/ http://www.domain.com/404/

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!

oheon
05-20-2010, 03:05 PM
The solution can be found using C-panel.

createdevelop
07-12-2010, 06:26 PM
So let me get this straight, you don't want /pages/ to be accessed but you do want the child pages accessed?

Why don't you use the wp_list_pages('exclude=xxxxx') for the menus to make sure your chosen pages are not listed. You could then use the 301 redirect plug in to redirect the pages to a 404 page. In my opinion it would be better to have those parent pages with some content, even if it is just a list of the child pages.

I hope that answers your question.

Social-Media
07-15-2010, 12:24 PM
Why on earth would you 301 redirect to a page that is supposed to throw a 404 status? No page on you site should be "Moved Permanently" to a 404 page address. If you're wanting to display the 404 page when someone accesses those pages then that is one thing, but don't 301 redirect them there. Use URL rewriting so that the 404 page gets displayed but http://www.example.com/pages/ or http://www.example.com/products/ remains in their browser address bar. Be sure that the 404 page returns a 404 status.

Something like this should work in the .htaccess file in the root of the web:

RewriteCond ^pages(/)?$ [NC]
RewriteRule (.*) http://www.example.com/404/ [L]

RewriteCond ^products(/)?$ [NC]
RewriteRule (.*) http://www.example.com/404/ [L]

The whole idea of having URLs that contain nodes in the path (like /pages/ and /products/) that really don't exist is a terrible design IMO. Lots of users will "chop off" the subpage name at the end of the URL to navigate up a level, in which case you're going to be sending them to a 404 page.

A better option might be to actually build out a parent page for /pages/ and /products/ these pages could simply talk about the company or various gadgets at a high level, and include contextual links to the sub-pages as well. You don't have to provide a link on the site to the parent pages, but if someone does happent to navigate to it by chopping off the sub-page name from the URL then at least there is a page there for them to see instead of them been shown a 404 as if they had done something wrong.