Mobile app version of vmapp.org
Login or Join
Correia994

: Redirecting all child pages from subdomain I can't get this redirect working... Can someone point me in the right direction please? On a WordPress site I want to prevent access to all pages

@Correia994

Posted in: #Redirects #Subdomain #Wordpress

I can't get this redirect working... Can someone point me in the right direction please?

On a WordPress site I want to prevent access to all pages that sit beneath a given parent page. All I want people to see is that parent page.

So if example.com/subdomain/parent-page is made up of:

parent-page
parent-page/sub-page-1
parent-page/sub-page-2
...and so on


I want all matching sub-pages redirecting to example.com/subdomain/parent-page.

My .htaccess file sits in the root folder of example.com/subdomain.

I think I need RedirectMatch for this so I can use regex.

But all my variations of:

RedirectMatch 301 parent-page/?* example.com/subdomain/parent-page


Do not work for one reason or another. The best result I've had is redirecting pages to the parent domain's "page not found" page!

Clearly I'm missing something here?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Correia994

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

On a WordPress site


WordPress uses mod_rewrite to handle the URL routing (pretty URLs) - which I assume you are using - so you should avoid using a mod_alias redirect (Redirect, RedirectMatch, etc.) in this instance. (Different modules run at different times, regardless of the order of the directives in the .htaccess file, so you can get unexpected results/conflicts. mod_rewrite usually runs before mod_alias.)

In .htaccess it's easy enough to redirect example.com/subdomain/parent-page/<anything>, but if you only want to redirect existing WordPress pages and 404 when that page does not exist then that is more complex and you should probably seek a WP solution, rather than .htaccess.

Try something like the following at the top of your example.com/subdomain/.htaccess file. These directives must come before any existing WordPress routing directives.

RewriteEngine On
RewriteRule ^parent-page/. /subdomain/parent-page [R=302,L]


Change the 302 (temporary) redirect to 301 (permanent) when you are sure it's working OK. (301's are cached by the browser so can make testing problematic.)

Any URL that starts /subdomain/parent-page/<something> will be redirected (regardless of whether that sub-page exists or not). This also helps to avoid a redirect loop.

RewriteEngine only needs to appear once in the .htaccess file - preferably (more logical) at the top.




RedirectMatch 301 parent-page/?* example.com/subdomain/parent-page


Aside... There are a few things wrong here. The regex is not strictly valid. * matches the preceding char 0 or more times (the preceding char is ?, which is itself a meta character). If you are specifying the domain in the target URL then it must be absolute with protocol etc.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme