An Apache 301 redirect for a deleted directory

I recently reorganized the devdaily.com website, and as part of that, I deleted an entire directory at the root level that was named "/Dir". This was the "directory", where I had a Yahoo-like directory of links to applets, tutorials, CGI scripts, and so on, so I had thousands of web pages with URLs like these:

http://www.devdaily.com/Dir/Java/Vendors/Applets/

When I deleted the "/Dir/" directory, I knew that users would suddenly get 404 errors for thousands of pages that used to exist. I decided I wanted to do a 301 redirect from that deleted directory to a new web page where I briefly described what I had done. I thought this was a much better approach than letting users see a 404 page. An SEO friend of mine tells me this can also be good for SEO, and keeping search engines happy with your website.

So, as a quick summary, my goal was:

Redirect all content from my old, deleted directory structure to one new web page.

My Apache 301 redirect statement (RewriteRule)

In short, after doing a bunch of research, and some trial and error, I finally came up with the following Apache 301 redirect statement, which solved my problem:

# added 2011/05/24 to account for deleting /Dir
# need to put this one first. the 'L' means that if this matches, don't try any more rules.
RewriteRule ^Dir(.*) http://www.devdaily.com/news/2011/directory [R=301,L]

I put this Apache "RewriteRule" in my httpd.conf file, ran this command to make sure it validated okay:

apachectl configtest

and when I got an "ok" message, I restarted my Apache server like this:

apachectl restart

Walking through my Apache RewriteRule

The Apache RewriteRule statement can be described like this:

  • For any URI that begins with "Dir/"
  • Send the user to the web page http://www.devdaily.com/news/2011/directory instead
  • This is a "301 redirect" , meaning it's a permanent redirect
  • The 'L' in the brackets at the end means that if this rule matches the URI, it is the "last" rule, and Apache should not consider any other Redirect or Rewrite rules.

That last rule was huge for me. Because I use Drupal for this website, it requires a number of other Apache RewriteRule statements, and when I first started, I had my new statement at the end of the other RewriteRule statements. Once I learned about this 'L' flag, I (bravely) made this Rewrite rule my first RewriteRule, then restarted Apache, and after some testing, I was able to confirm this was the right approach.

(You can read about the 'L' rule at this Apache RewriteRule web page.)

My Apache 301 redirect rule for a deleted directory - Summary

In summary, if you need to created a 301 Redirect from a deleted directory structure to one single web page (for SEO purposes, or just to keep readers happy), this rewrite rule should work for you. As a final note, it's extremely important to test your website after making a change like this. A small mistake in an Apache RewriteRule could have really bad consequences.