![]() |
||||||||||||
|
||||||||||||
|
Razer Technology Solutions, Inc.
|
Module mod_rewrite
Tutorial (Part 2): Rule Conditions In this tutorial's
last instalment we started off with a discussion of the basics of Module
"If access to file .htaccess is attempted, return an error message stating that access is denied." This rule is valid globally, i.e. everyone will receive the specified error message. We can, however, restrict
a rule by what is termed "rule conditions" - in this case, the
rule will only Syntax: The condition must precede the rule! Let us explain this procedure with an example. The lines below are entries in file ".htaccess". RewriteEngine
on The first three lines
were covered in detail in Part 1 of this tutorial. Their function is to
initialize the Our line: RewriteCond %{HTTP_USER_AGENT} ^EmailSiphon is made up of the following three parts: Directive: RewriteCond The TestString is a server variable which is written in the general form of "%{NAME_OF_VARIABLE}". In our example we have defined the "HTTP_USER_AGENT" as "NAME_OF_VARIABLE". CondPattern is a regular
expression. Before we continue with its specifics, let us take a Regular expression Regular expressions are similar to a small, compact programming language in its own right. E.g. the regular expression "s/abc/xyz/g" will globally replace the string "abc" in a text by "xyz". Here is an overview of the most important elements with some examples: .(dot) - text (any
character) Regular expressions are construed with the help of these elements and alphanumeric characters. Regular expressions
are not used isolated by themselves; instead, they are integrated in other In connection with
Module mod_rewrite they are used in the directives RewriteRule and RewriteCond. But as this particular
regular expression doesn't contain the character "$" (end of
line anchor), the
defines what will happen when a spider is requesting access. The regular expression "^.*$" signifies: If access to any file is requested, the error message "forbidden" will be displayed. The dot "." in the regular expression is a meta symbol (wildcard) and signifies any random character. "*" signifies
that the string may occur an unlimited number of times. In this case,
regardless which
So let's say we want
to fend off this spider as well. In this case we will require another
condition to be This gives us the following entries to file ".htaccess": RewriteEngine on The third argument ([OR]) in line: RewriteCond %{HTTP_USER_AGENT} ^EmailSiphon [OR] is termed a "flag".
In regard to conditions there NC (no case) Flag "NC"
permits case insensitive testing of the
RewriteCond %{HTTP_USER_AGENT} ^emailsiphon [NC] This line specifies that both "emailsiphon" and "EmailSiphon" shall be recognized. If you wish to use multiple flags, you may delimit them by commas. RewriteCond %{HTTP_USER_AGENT}
^EmailSiphon [NC,OR] There are no restrictions
to the number of conditions. Thus, you could block 10, 100, 1000 or more In the above example, the string "HTTP_USER_AGENT" is being used. Further server variables are: REMOTE_HOST For example, if you
want to block the spider comming from < www.cyveillance.com >, you
will use variable RewriteCond %{REMOTE_HOST}
^www\.cyveillance\.com$ The dot "."
in the domain name must be protected by "\" (backslash), otherwise
it would be handled like If you want to block any given IP, the condition will read: RewriteCond %{REMOTE_ADDR}
^216\.32\.64\.10$ In the regular expression, enter the IP in its entirety, delimited by the line anchors. You may even exclude a whole IP range from access: RewriteCond %{REMOTE_ADDR}
^216\.32\.64\. This example will cover all individual IPs from "216.32.64.0" through "216.32.64.255". Here's a little teaser
quiz for you to check out your skills. The solution will be featured in
the next RewriteCond %{REMOTE_ADDR}
^216\.32\.64 Quiz Question Up until now we have
used a simple RewriteRule which will generate an error message. In the
This text may freely be republished or distributed provided the following resource box is included intact either at the beginning or the end of the article and a complimentary copy or notice (link) is sent to the author at the address specified below: Dirk Brockhausen is
the co-founder and principal of fantomaster.com Ltd. (UK) and fantomaster.com
GmbH |
|
|||||
| home · services
· about us · copyright
· the eZine · contact
· customer site Copyright © Razer Technology Company 1999 - 2008. Legal Disclaimer Site Mod:1/2008. |