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

Redirecting Subdirectories to the Root Directory via HTAccess

One of the most useful techniques in my HTAccess toolbox involves URL redirection using Apache’s RedirectMatch directive. With RedirectMatch, you get the powerful regex pattern matching available in the mod_alias module combined with the simplicity and effectiveness of the Redirect directive. This hybrid functionality makes RedirectMatch the ideal method for highly specific redirection. In this tutorial, we will explore the application of RedirectMatch as it applies to one of the most difficult redirect scenarios: redirecting all requests for a specific subdirectory (or any subordinate directory or file) to the root (or any parent) directory. We will explore how to accomplish this redirect using PHP in a subsequent article.

The Scene

When developing sites, an excellent way to maintain a clean directory structure involves using subdirectories to organize content. A great example of this is seen in the placement of a site’s blog into its own subdirectory, such as this:

http://domain.tld/blog/

In this scenario, the blog directory serves as the home page for the blog itself. This works great if the content displayed on the site’s home page (i.e., at http://domain.tld/) is meant to be different than that displayed on the blog’s home page. So, for example, your site’s main page would greet visitors with a few photos and a nice welcome message. Then, from the welcome page you would link to your blog, which features your posts, pages, and other content.

Conversely, many sites prefer to display their blog content on the home page, or root, of the site. This, of course, is easily accomplished by simply placing your blog in the root directory. If you aren’t planning on growing, expanding, or restructuring your site in the future, and aren’t really concerned with maintaining a clean, organized directory structure, then by all means, go ahead and throw your life away by installing your blog in the root directory. If, on the other hand, you would like to display your blog content on the site’s home page, but would prefer to give the blog its own directory, read on..

The Challenge

The challenge facing the described strategy is duplicate content. By serving your blog from its own directory and delivering its content at the root directory, duplicate content is available in the following locations:

http://domain.tld/
http://domain.tld/blog/

When users visit either of these URLs, they will see the same content, namely, the main index page of your blog. This is a convoluted scenario for human visitors and a potential penalty from the search engines as well. Indeed, it would be far better to either redirect the blog root to the site root or vice versa. Logically, of course, it makes more sense to use your domain as the home page, so we will craft our HTAccess solution based on the following configuration:

  • Blog installed/located in its own (sub)directory
  • Blog content displayed on the site’s home page
  • Requests for the blog directory redirected to the home page

Once established, this configuration will enable you to place your blog in its own directory and display your blog on the home page (root URL) of your site. Further — and this is the trick — only the /blog/ directory will be redirected to the home page; all other blog pages will continue to be available in their expected locations. Here is a sampling of hypothetical URLs demonstrating this configuration:

  • http://domain.tld/
    [home page of site, shows blog content, no redirect]
  • http://domain.tld/blog/
    [blog subdirectory, redirected to home page]
  • http://domain.tld/blog/an-example-post/
    [blog post, shows blog directory in URL]
  • http://domain.tld/blog/an-example-page/
    [blog page, shows blog directory in URL]
  • http://domain.tld/blog/an-archive-page/
    [archive page, shows blog directory in URL]

Bottom line: this redirect strategy facilitates subdirectory blog installation, eliminates duplicate content, and enables the display of your blog on the home page of your site. Let’s have a look..

The Solution(s)

Here are two possible solutions for this scenario..

WordPress

Depending on your server configuration and blogging platform, there are several ways to implement this strategy. First of all, WordPress provides a “built-in” mechanism for giving your blog its own directory. This method works best if implemented during or immediately after the installation of WordPress. With this technique, WordPress rewrites all of your permalinks such as to remove the /blog/ portion of your URLs. Thus, if you are launching a new WordPress-powered blog, this strategy is aesthetically superior to the HTAccess solution by simply eliminating all references to the blog’s subdirectory; it will be as if you had installed your blog in the root directory to begin with.

But keep in mind that there are potential shortcomings with the WordPress method, such as are encountered when trying to integrate additional content to your site. WordPress’ activated rewrite mechanism may interfere with the proper functionality of galleries, blogs, and other peripheral content. It should also be noted that implementing WordPress’ redirection feature will break any external links to your blog’s pages. Depending on the number of inbound links, this could result in a large number of 404 errors. But relax! If all of this WordPress redirection stuff makes your skin crawl, read on for a better solution.

Apache/HTAccess

In general, I prefer to handle redirection at the server level. Using Apache’s powerful rewrite functionality, it is possible to craft highly specific redirects for virtually any configuration. In this case, where we need to redirect requests for the /blog/ (sub)directory to the site’s root directory, Apache’s RedirectMatch directive accomplishes the task perfectly. Given the previously described scenario, the following Apache directive should be placed in the site’s root HTAccess file (or, alternatively, in the server’s configuration file):

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

Pure magic. Once in place on an Apache-powered server, this single line will redirect all requests for the /blog/ directory to the site’s root directory. As outlined previously, all subordinate URLs will include the /blog/ directory in the address string and continue to function as expected. This is a concise, direct, effective solution that is as simple as possible. Only a single, target URL is affected, enabling you to easily integrate new features into your site without overzealous rewrite interference.

As for the functional specifics of the RedirectMatch technique, the process is very straightforward:

  1. Match all URLs containing the specified character string
  2. Redirect all matches to the specified target URL
  3. Deliver a redirect status of permanent (301) with all requests

Also, notice how RedirectMatch differs from the similar Redirect directive. With RedirectMatch, exact pattern matching is possible using regular expressions. Conversely, Redirect uses prefix matching, which affects any URL that includes the specified character string. Regular expressions aren’t allowed in Redirect directives, but they are allowed with RedirectMatch.

Variations

In addition to redirecting the subdirectory of your blog to the site’s root directory, you can also use the RedirectMatch directive for many other case-specific redirects. For example, I recently shared a technique for redirecting all requests for a nonexistent file to the actual file. In that article, I prescribe a technique for redirecting all misdirected requests for the site’s favicon.ico back to the actual file located in the root directory of the site:

# REDIRECT FAVICON REQUESTS
<ifmodule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC]
 RewriteCond %{REQUEST_URI} favicon\.ico [NC]
 RewriteRule (.*) http://domain.tld/favicon.ico [R=301,L] 
</ifmodule>

Of course, this method works completely well, but may be simplified greatly using our new friend, RedirectMatch:

RedirectMatch 301 ^/favicon.ico$ http://domain.tld/favicon.ico

Comparing the two different techniques reveals a wealth of information, and I highly encourage it. But, rather than get into all of that here, let’s move on with another variation.

Unlike the redirect scenario addressed in the article, let’s imagine a case where we would like to redirect not only the blog directory, but all other files and subdirectories as well, such that:

  • http://domain.tld/blog/
  • http://domain.tld/blog/file-01.html
  • http://domain.tld/blog/file-02.hmtl
  • http://domain.tld/blog/sub-01/
  • http://domain.tld/blog/sub-02/

..and etc. will all be redirected to some specified target location. This target location can be anything — the home page, a single file, another subdirectory, etc. — and on any server. The possibilities and uses for such a redirect are endless. Here is how it looks in the HTAccess file:

RedirectMatch 301 ^/blog/.*$ http://domain.tld/target.html

Other variations on this technique include specifying temporary (302) redirects rather than permanent (301) by editing as follows:

# This is a permanent redirect
RedirectMatch 301 ^/blog/.*$ http://domain.tld/target.html
# This is a temporary redirect
RedirectMatch 302 ^/blog/.*$ http://domain.tld/target.html

Likewise, you may also write:

# This is a permanent redirect
RedirectMatch permanent ^/blog/.*$ http://domain.tld/target.html
# This is a temporary redirect
RedirectMatch temp ^/blog/.*$ http://domain.tld/target.html

The End (for now..)

You know I could on and on with this stuff, but I am getting hungry now, so I will leave it here and grab a sandwich. I may sharpen things up a bit or add some more once I get back, but probably not. Instead, I think I will just call it good and see if anyone actually makes it through the entire article to read this. My money says that a few will, but not the majority. I mean, come on, who has time to waste with all of this geeky nonsense anyway? Certainly not me! ;)

About the Author
Jeff Starr = Fullstack Developer. Book Author. Teacher. Human Being.
Digging Into WordPress: Take your WordPress skills to the next level.

33 responses to “Redirecting Subdirectories to the Root Directory via HTAccess”

  1. very useful article, i was actually pondering along the same lines today, but I was thinking how it wold be possible to redirect multiple domains to one folder.

    So ‘public_html/’ was the same for domain1.com as well as domain2.net domain3.info etc

    this resulting in domain1.com/apage.php would be the same as domain2.net/apage.php.

    any thoughts how trickery could be used here? i was thinking using a 301 with systemlinks forwarded … but wasn’t sure if it would work

  2. Joshua Richardson 2008/10/06 8:43 pm

    Off-topic but the linked names in your comments on this site aren’t nofollowed anymore – if this is on purpose then disregard this. I just thought you might not have noticed since the redesign.

  3. Jeff Starr 2008/10/06 9:29 pm

    Hi Joshua, yes that is correct. Actually, I stopped nofollowing commentator links over a year ago. Here is the article that explains my reasoning for dropping nofollow and joining the dofollow movement. Thanks for keeping an eye on things around here — it is much appreciated!

  4. James D Kirk 2008/10/07 9:56 am

    First, thanks for the “DoFollow”; it all really helps, especially if one reads blogs like yours and Andy Beards and learns how to PR sculpt the flow into and out of their web sites.
    Second, if people or ordinary powers (or less in my case ;) ) have ever tried to teach themselves all about Regex; how to write it, how to “speak” it, then you’ll know that this article is the kind that just absolutely gets bookmarked in your “reference” folder. This stuff probably isn’t “rocket science” but it’s pretty darned close!
    Third, I did make it all the way through the article!

  5. Jeff Starr 2008/10/07 5:25 pm

    Hi James, thanks for the positive feedback. You are absolutely correct, dofollow does help, especially in today’s fierce linking economy. Anything I can do to help kind folks such as yourself promote their sites is a great pleasure. Also, thanks for reading the entire article! I know that this rewrite stuff is pretty dry and often frustrating to understand, but I do my best to make it all go down smooth with clear tutorials and an upbeat writing style. Not always a smashing success in this regard, but kind comments such as yours certainly encourage me to keep it up! Thanks! :)

  6. Jeff Starr 2008/10/07 5:31 pm

    @Donace: kind of confused as to what you mean here.. couldn’t you simply forward the alternate domains to the one with the desired content?

  7. Joshua Richardson 2008/10/07 5:33 pm

    I just had a thought, perhaps something really cool would be a .htaccess tool for people to generate their files – maybe written in javascript. Something that includes difficult things like mod_rewrite and caching options.
    I know alot of people get stuck trying to write regular expressions and it can be fun debugging them sometimes!

    I know these sorts of tools exist for .htaccess files but I’ve never seen any for mod_rewrite rules.

  8. James D Kirk 2008/10/07 6:06 pm

    Hey Jeff,
    When you come across good, clear writing that explains tough topics well, you just have to talk well about it. And you are SO right about how dry this stuff can be. I can remember a few years ago spending literally hour after hour in the Apache documentation in an effort to just understand how regex stuff worked. And then going to to the site of the fellow that actually created most of that tech (though, I’ve totally forgotten his name! Bad me!)

    And with your permission, I’d point @Donace towards a great WordPress plugin that will totally do what he wants done. It’s called Domain Mirror (404 link removed 2012/08/03) and should work well, though I have to admit I’ve yet to try it on WP 2.5 or above. But if it does work, there’s a nice companion WP plugin that one would likely want to use as well called Google XML Sitemaps Mirror which will create the proper sitemaps in XML, when using the Domain Mirror plugin. Definitely worth the read of those two sites, if nothing else.

    Thanks!

  9. @ jeff redirecting to a domain would be no fun:p i woul want each domain to hae its on ‘urls’…think along th lines of a proxy site…they all have basically the smae content but just need to show diffrent domain names thoughout your stay there.

    @James…cheers for that I’ll give it a look

  10. James D Kirk 2008/10/08 9:53 am

    @Donace The plugin Domain Mirror will do exactly what you are asking for.

    Cheers!

  11. sweet james…i’ll dig into the code then because I need it for a non-wordpress site :p

  12. Christopher Ross 2008/10/11 4:14 pm

    Awesome post! .htaccess is one of the most useful tools on a linux server.

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 »
USP Pro: Unlimited front-end forms for user-submitted posts and more.
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.