Using sed to enable .htaccess files in Apache config

13

6

I am trying to automate the provisioning of an Ubuntu Vagrant box with a Bash script. All is well, but I am having difficulty with configuring Apache to allow overrides from .htaccess files. In my config file, there is the following block:

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

All I need to do is replace AllowOverride None with AllowOverride all. This seems to be a task for sed, but there are other <Directory> blocks in this config file where I do not want to allow override.

How can I use sed to replace a specific string with a new string, when that specific string occurs between two other specific strings?

In other words, how can I replace AllowOverride None only when it appears between <Directory /var/www/> and </Directory>?

I can use the following command to get the block I am interested in:

sed -n '/<Directory \/var\/www\/>/,/<\/Directory>/p' 000-default

Brad

Posted 2013-09-15T20:19:42.447

Reputation: 4 459

Answers

13

sed supports pattern ranges in this form:

sed '/startpattern/,/endpattern/ <sed-commands>' file

For example:

sed '/<Directory \/var\/www\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride all/' httpd.conf

suspectus

Posted 2013-09-15T20:19:42.447

Reputation: 3 957

This is what I was looking for! – captainblack – 2019-06-11T09:40:49.823