[ Team LiB ] |
![]() ![]() |
A.1 What Directives Use Regular Expressions?Two main categories of Apache directives use regular expressions. Any directive with a name containing the word Match, such as FileMatch, can be assumed to use regular expressions in its arguments. And directives supplied by the module mod_rewrite use regular expressions to accomplish their work. For more about mod_rewrite, see Chapter 5. SomethingMatch directives each implement the same functionality as their counterpart without the Match. For example, the RedirectMatch directive does essentially the same thing as the Redirect directive, except that the first argument, rather than being a literal string, is a regular expression, which will be compared to the incoming request URL. A.1.1 Regular Expression BasicsTo get started in writing your own regular expressions, you'll need to know a few basic pieces of vocabulary, such as shown in Table A-1 and Table A-2. These constitute the bare minimum that you need to know. Although this will hardly qualify you as an expert, it will enable you to solve many of the regex scenarios you will find yourself faced with.
A.1.2 ExamplesThe previous concepts can best be illustrated by a few examples of regular expressions in action. A.1.2.1 Redirecting several URLsWe'll start with something fairly simple. In this scenario, we're getting a new web server to handle the customer support portion of our web site. So, all requests that previously went to http://www.example.com/support/ will now go to the new server, http://support.example.com/. Ordinarily, this could be accomplished with a simple Redirect statement, but it appears that our web site developer has been careless and has been using mod_speling (see Recipe 5.10), so there are links throughout the site to both http://www.example.com/support/ and to http://www.example.com/Support/, which would actually require not one but two Redirect statements. So, instead of using the two Redirect statements, we will use the following one RedirectMatch directive: RedirectMatch ^/[sS]upport/ http://support.example.com/ The square brackets indicate a character class, causing this one statement to match requests with either the upper- or lowercase s. Note also the ^ on the front of the argument, causing this directive to apply only to URLs that start with the specified pattern, rather than URLs that simply happen to contain that pattern somewhere in them. A.1.2.2 Catching common misspellingsWhile watching the logfiles, we see that a number of people are misspelling support as suport. This is easily fixed by slightly altering our RedirectMatch directive: RedirectMatch ^/[sS]upp?ort/ http://support.example.com/ The ? makes the second p optional, thus catching those requests that are misspelled and redirecting them to the appropriate place anyway. A.1.3 For More InformationBy far the best resources for learning about regular expressions are Jeffrey Friedl's book Mastering Regular Expressions and Tony Stubblebind's book Regular Expression Pocket Reference, both published by O'Reilly. They cover regular expressions in many languages, as well as the theory behind regular expressions in general. For a free resource on regular expressions, you should see the Perl documentation on the topic. Just type perldoc perlre on any system that has Perl installed. Or you can view this documentation online at http://www.perldoc.com/perl5.6.1/pod/perlre.html. But be aware that there are subtle (and not-so-subtle) differences between the regular expression vocabulary of Perl and that of Apache. |
[ Team LiB ] |
![]() ![]() |