Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Parsing/validating Apache .htaccess file

by pingo (Hermit)
on Aug 09, 2005 at 11:27 UTC ( [id://482172]=perlquestion: print w/replies, xml ) Need Help??

pingo has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Does anyone know of a module to parse and/or validate an Apache .htaccess file? What I want to do is to automatically (with a cron job) block certain ip numbers, and the only way I can think of is to put a RewriteCond in the appropriate .htaccess file. Now, this isn't particularly difficult, of course, but if I managed to screw that file up, it would be bad...

(In case anyone is wondering, the cron job loops through the access log and generates a list of ip numbers that seem to be bulk downloading content from our site.)
  • Comment on Parsing/validating Apache .htaccess file

Replies are listed 'Best First'.
Re: Parsing/validating Apache .htaccess file
by reneeb (Chaplain) on Aug 09, 2005 at 11:51 UTC
Re: Parsing/validating Apache .htaccess file
by izut (Chaplain) on Aug 09, 2005 at 12:05 UTC

    You can have a htaccess.tpl and then process it using some templating toolkit of your choice, like Template-Toolkit. It is easier to mantain this way IMHO.

    Example:
    use strict; use warnings; use Template; my $template = Template->new(); my $vars = { ips_to_block => [qw/10.10.10.1 10.10.11.1/], }; my $output = ''; $template->process(\*DATA, $vars, \$output) or die $template->error(); print $output; __DATA__ AuthUserFile /var/apache/htpasswd AuthGroupFile /var/apache/htgroup AuthName "My Realm" AuthType Basic order allow,deny allow from all [% FOREACH ip_to_block = ips_to_block %] deny from [% ip_to_block %] [% END %]
    Hope this helps.

    Update:Removed <Limit> tags. Thanks merlyn!


    Igor S. Lopes - izut
    surrender to perl. your code, your rules.
      <Limit GET POST>
      Stop limiting the limits! Remove that line (and the closing line). Otherwise, your restrictions are not applying to the other half dozen methods that can used in that area.

      It's probably not your fault. One of the early Apache docs showed <limit GET POST> and everyone picked up on it, but of course that was revised later, but by then everyone was cargo-culting the broken thing.

      From the apache docs:

      Access controls are normally effective for all access methods, and this is the usual desired behavior. In the general case, access control directives should not be placed within a <limit> section.

      So, one of my many personal crusades is to remove this cargo cult by telling people: Don't limit your limits!.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://482172]
Approved by Nevtlathiel
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found