Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Parsing/validating Apache .htaccess file

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

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


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.

    use strict; use warnings; use Template; my $template = Template->new(); my $vars = { ips_to_block => [qw/], }; 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?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2020-12-05 08:16 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (63 votes). Check out past polls.