apache virtual host rename all *:80 *:443 httpd.conf

July 3, 2011

I needed to rename a bunch of apache virtual hosts to *:80 and *:443.

Here’s an example that renames /etc/httpd/conf.d/httpd.conf to httpd.confr and changes :80 and :443 to *:80 and *:443:

# sed -ir 's/VirtualHost [0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}:80/VirtualHost *:80/' /etc/httpd/conf/httpd.conf

# sed -ir 's/VirtualHost [0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}:443/VirtualHost *:443/' /etc/httpd/conf/httpd.conf

To apply the same changes to *.conf in a specific directory:

for i in `ls /etc/httpd/conf.d/`
do
sed -ir 's/VirtualHost [0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}:80/VirtualHost *:80/' /etc/httpd/conf.d/$i
sed -ir 's/VirtualHost [0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}:443/VirtualHost *:443/' /etc/httpd/conf.d/$i
done

How do we view the sweet results of our handywork?

# find . -name "*.conf" -exec egrep -H "VirtualHost ([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}" '{}' \; -print

Also you can use this (shows all lines that contain *:80 or *:443 in any file in current directory or subdirectory):

# grep -r ":443" .;grep -r ":80" .

And what the hell do you do with all these .confr files when you are ready to rename them all to .conf ? You run this script to rename them to .conf:

for i in `ls /path/to/confr/files`; do mv "$i" "`basename $i .confr`.conf"; done

And that’s how you do a apache virtual host rename all *:80 *:443 httpd.conf