Redirecting all child pages from subdomain

Redirecting all child pages from subdomain - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the Redirecting all child pages from subdomain error on your web browser. Problem :


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?


Solution :


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.


We hope that this article has helped you resolve the redirects, wordpress, subdomain error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

How do I redirect traffic only if being accessed from a specific port?

DNS entry to redirect constant url to changing directory name

How to redirect to any domain [duplicate]