Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
Web Dev + WordPress + Security

Redirect any Subordinate URL to its Parent Directory via PHP

Simple one for you today. After posting on how to use HTAccess to redirect subordinate URLs to the root (or parent) directory, I thought I would share an alternate way of accomplishing the same trick using PHP. Fortunately, using this PHP redirect technique doesn’t require access to or fiddling with your site’s HTAccess (or Apache configuration) file and it is very easy to implement.

The scene, as discussed in greater detail in my previous article on this topic, involves a very specific type of redirect whereby some target URL is redirected up the directory structure to one of its parent directories. Redirecting such subordinate content generally falls into one of two categories:

  1. The target file/folder and all of its subordinate content needs to be redirected higher up the directory structure
  2. The target file/folder and only the targeted file or folder needs to be redirected higher up the directory structure

An example of the first case is seen when redirecting an entire blog to the root directory, and is trivial to accomplish via HTAccess:

Redirect 301 /blog/ http://domain.tld/

With this, all blog URLs will be redirected to the root directory. The Redirect directive matches any URLs containing the character string /blog/, which includes every post, page, and other web document contained within the blog directory. Plain and simple, no frills, no fuss.

On the other hand, the second case is more complicated in that we are only targeting a specific file or folder for redirection. In this case, we want to match only /blog/, say, and not any of its subordinate content. This is impossible to accomplish using Apache’s Redirect directive because of its greedy matching functionality. Instead — and again, see my previous article for more information on this subject — targeted redirection of specific subordinate content via HTAccess requires an alternate directive, namely, RedirectMatch:

RedirectMatch 301 /blog/ http://domain.tld/

With RedirectMatch, we are able to precisely target the specific directory by matching the exact character string, /blog/, and nothing beyond that. This technique is also perfect for redirecting in the opposite direction — e.g., redirecting the root URL to a subdirectory — without invoking the always undesirable infinite loop problem.

In any case, I can see I am getting carried away again.. After all, this article is about how to accomplish this redirect using PHP..

Redirect Subordinate Content via PHP

Now that I have your attention, I should mention that either of these redirecting techniques (i.e., PHP or HTAccess) works great for just about any type of resource-specific URL redirection. They are by no means limited to the marketed case: redirecting subordinate content higher up the directory structure. As many of you know, redirecting targeted content down the directory structure can be just as challenging — unless you have the proper tools. So, for the ever-learning developer in you, here is another essential redirect tool for your utility belt:

<?php // Permanent 301 Redirect via PHP
	header("HTTP/1.1 301 Moved Permanently");
	header("Location: http://domain.tld/");
	exit();
?>

At this point, you may be think, “yeah, I already knew that.” If so, good for you. I knew it as well, yet long before realizing its usefulness in handling the granular-type redirects discussed previously. The effectiveness and ease of this technique involves its functionality and implementation. Rather than pattern matching against some target character string, the method functions according to its location. That is, instead of targeting some resource virtually (via HTAccess regex matching), we apply the redirect by placing the function directly in the target resource. This way, only the target file(s) or folder(s) is redirected, everything else remains unaffected.

Hopefully, you already know how to implement this little snippet of PHP goodness; if not, check it out,it couldn’t be easier:

  • Edit the “domain.tld” to the desired redirect location
  • Place the now-edited code into the very beginning of your target file
  • For directories, place the code into a blank file named “index.php” and put the file into the target directory

That’s it. Upload, test, and enjoy. As always, if you have any questions or concerns, please let me know via the comments section below and I will do my best to help you out.

Thanks for your generous attention! :)

About the Author
Jeff Starr = Fullstack Developer. Book Author. Teacher. Human Being.
.htaccess made easy: Improve site performance and security.

7 responses to “Redirect any Subordinate URL to its Parent Directory via PHP”

  1. Web Design Sussex 2009/01/26 2:18 am

    Thankyou for this great writeup!

  2. Web Design Directory UK 2009/01/26 2:26 am

    I love PHP and the way it allows such control over the server it is running on – unforunately this can also lead to it being hacked (this happened to me last year). Hopefully I’ve learnt from it though, it’s articles like this which teach me how to secure my server so many thanks!

  3. Sure thing, but take it easy with the borderline-spammy comments. Thanks.

  4. Hi, I’m wondering if this technique can be used for my situation.
    Let me try to explain what I have and what I need to do.

    I’ve got a site ‘B.com’ that works fine. Now I’ve bought another domain ‘A.com’ that is parked at B.com.
    Without buying another account, I simply want visitors to A.com to be redirected to a particular page under B.com.
    I’m told I have to use Apache hacks to do that, but that’s way farther than I am capable of going (I presume).
    I’m a php newbie for sure, and it seems there should be some easy way to simply trap for A.com as the url but I can find how.
    thanks for any help.
    -Evan

  5. Jeff Starr 2009/05/27 8:47 am

    @Evan: Have you tried something like this:

    RedirectMatch 301 / http://www.B.com

    Place in the active web-accessible root HTAccess file of domain A.com and you should be good to go (unless I have missed something here).

  6. Alejandro Leal 2010/05/06 6:51 pm

    Can you illustrate the procedure to implement this trick with an exception?

    So target subdirectory to redirect is:

    Redirect 301 /blog/ http://domain.tld/

    So most URLs under that subdirectory will be redirected to the domain.tld root.

    But, I’d like for domain.tld/blog/keep-me/ to *not* redirect…

    Thanks for the great tutorial!

  7. Jeff Starr 2010/05/06 9:34 pm

    Alejandro Leal, we could use mod_rewrite for this:

    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{THE_REQUEST} !/keep-me/ [NC]
      RewriteRule (.*) http://domain.tld/$1 [R=301,L]
    </IfModule>

    Place that in the /blog/ directory, and it should redirect only non-keep-me requests to the root directory.

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
The Tao of WordPress: Master the art of WordPress.
Thoughts
I disabled AI in Google search results. It was making me lazy.
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources. Also: “The Web” is not a valid answer.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.