Apache RedirectMatch wildcard examples

Apache Redirect 301 FAQ: How can I redirect many old web pages using the Apache Redirect or RedirectMatch syntax and wildcard patterns (regex patterns)?

I'm currently trying to fix a lot of URLs that I more or less intentionally broke when I deleted the old "directory" portion of this website. In short, after removing the directory, no URL at "/Dir" work any more, so I have thousands of broken URLs (technically "URIs") that look like this:

/Dir
/Dir/
/Dir/Perl
/Dir/Java/
/Dir/Java/Applets

After breaking all these URLs, I'm now trying to "fix" them by sending them to new, valid web pages.

Because I have so many broken links, I don't want to use the normal Apache 301 Redirect command syntax, and instead I wanted to use the Apache RedirectMatch syntax.

Simple Apache RedirectMatch example

As a quick Apache RedirectMatch example, I just fixed all of my old Perl directory links like this:

RedirectMatch 301 /Dir/Perl/.* http://www.devdaily.com/perl

With that command, any visitor hitting any URL under the "/Dir/Perl" root will be redirected to my new Perl page. If you're used to wildcard characters in DOS or Unix, the ".*" wildcard character is very similar to the usual "*" wildcard you can use at the DOS or Unix command line. Here's a quick breakdown:

  • The "." means "any character".
  • The "*" means "zero or more of the preceding character".

As a result, this simple pattern is just like the usual DOS/Unix "*" wildcard. As a quick example of how this is more powerful, the pattern "[a-z]*" means "zero or more lowercase characters".

Again, for this example, you probably don't need to know that, but I thought I'd share it in case you have a situation like that.

Apache RedirectMatch - find and replace

In a more complicated example, you can also use the Apache RedirectMatch syntax to find patterns in the URL pattern you're trying to match, and then use what you found in the URL you're redirecting users to. You do this with a similar pattern:

(.*)

With this syntax, the ".*" part of the pattern again means look for any number of any character, and on top of that, the parentheses mean "remember whatever you found here so I can use it in my redirect URL".

For instance, in the follow RedirectMatch example, I'm telling Apache that it should remember whatever it finds in between my two search patterns, and use them as replacement patterns with my full URL (the $1 and $2):

RedirectMatch 301 /java/jwarehouse/org.eclipse.(.*)/(.*) http://www.devdaily.com/java/jwarehouse/eclipse/org.eclipse.$1/$2

So, if someone tries to go to a URL like this:

http://www.devdaily.com/java/jwarehouse/org.eclipse.FOO/BAR

my RedirectMatch example will redirect them to this URL:

http://www.devdaily.com/java/jwarehouse/eclipse/org.eclipse.FOO/BAR

Those URLs are fictitious, so you'll get error messages if you try to reach them, but I hope you get the idea of how this works. Perhaps a better example is if you just switched an old CGI or PHP program to a new URL structure, like this:

RedirectMatch 301 /handler.php?cat=(.*)&article=(.*) http://www.devdaily.com/$1/$2

With a command like that (again, a fictitious example) you could send a visitor from this URL:

http://www.devdaily.com/handler.php?cat=java&article=1001

to this one:

http://www.devdaily.com/java/1001

As you can see, this ability to find patterns in your "source" string and then use them in your "destination" URLs is a very powerful Apache RedirectMatch technique.

Apache RedirectMatch examples

In summary, I hope these Apache RedirectMatch pattern examples have been helpful. As you can see, you can either (a) perform a simple redirect on thousands of files with a pattern, or you can (b) perform a redirect while also remembering and copying portions of the given URL to the location where you want to redirect the visitor to.