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/");

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! :)