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:
- The target file/folder and all of its subordinate content needs to be redirected higher up the directory structure
- 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! :)
7 responses to “Redirect any Subordinate URL to its Parent Directory via PHP”
Thankyou for this great writeup!
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!
Sure thing, but take it easy with the borderline-spammy comments. Thanks.
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
@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).
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!
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.