Rundeck - apache_mod_rewrite.md

Given an Apache VHOST configuration file x.config.changed containing the following extra block:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^200.200.200.200
RewriteCond %{REMOTE_ADDR} !^192.168.1.
RewriteRule .* http://maintenance.x.x [R=302,L]
Header Set Cache-Control "max-age=0, no-store"
</IfModule>

The script below would add the a subnet 192.168.1 and an opitional IP to be excluded from the rewrite rule forwarding requests to a maintenance page:

#!/bin/bash

#Clean-up
sed -i '/RewriteCond/d' x.conf.changed

if [ -n "$1" ];then
INPUT="$(echo $1 | sed s/^/\!\^/g | sed s/\\./\\\\./g)"
sed -i '/RewriteEngine/a RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.' x.conf.changed
sed -i '/RewriteEngine/a RewriteCond %{REMOTE_ADDR} '$INPUT'' x.conf.changed
else
sed -i '/RewriteEngine/a RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.' x.conf.changed
fi

cp /x/x.conf.changed /etc/apache2/sites-available/x.conf

apache2ctl -k graceful

To revert it is just replace files as x.conf.changed and x.conf.original