Skip to content

Instantly share code, notes, and snippets.

@bmatthewshea
Last active April 22, 2019 20:59
Show Gist options
  • Save bmatthewshea/116ad1e08b54081b8358125bae7ca6eb to your computer and use it in GitHub Desktop.
Save bmatthewshea/116ad1e08b54081b8358125bae7ca6eb to your computer and use it in GitHub Desktop.
Create Apache redirects sites file from file list of subdomains
#!/bin/bash
inputfile=./subdomains-list.txt
outputfile=./my-apache.conf
host_names=""
# start fresh
if [ -a "$outputfile" ]
then
rm $outputfile
fi
#start site.conf by creating a 'header' - not really needed:
cat <<EOF >> $outputfile
# Brady Shea 27mar2019
# Global to this file
# Possible values include: debug, info, notice, warn, error, crit, alert, emerg
LogLevel warn
ErrorLog \${APACHE_LOG_DIR}/error-redirects.log
CustomLog \${APACHE_LOG_DIR}/access-redirects.log combined
EOF
# read subdomains/hosts from text file defined and add entries to the apache conf file..
# grep -vE '^(\s*$|#)' $inputfile
# = pipe without '#' comments and blank lines.
grep -vE '^(\s*$|#)' $inputfile | while read -r host_name
do
case "$host_name" in \#*) continue ;; esac # skip comments and blanks
if [ -w "$outputfile" ]
then
cat <<EOF >> $outputfile
<VirtualHost *:80>
ServerName *.$host_name.example.com
ErrorLog \${APACHE_LOG_DIR}/redirect.example.com-error.log
CustomLog \${APACHE_LOG_DIR}/redirect.example.com-access.log common
Redirect 301 / https://$host_name.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName *.$host_name.example.com
ErrorLog \${APACHE_LOG_DIR}/redirect.example.com-ssl-error.log
CustomLog \${APACHE_LOG_DIR}/redirect.example.com-ssl-access.log common
SSLEngine On
SSLCompression off
SSLProtocol all -SSLv2 -SSLv3
SSLCertificateKeyFile /etc/letsencrypt/live/wildcard.subdomains.example.com/privkey.pem
SSLCertificateFile /etc/letsencrypt/live/wildcard.subdomains.example.com/cert.pem
SSLCertificateChainFile /etc/letsencrypt/live/wildcard.subdomains.example.com/chain.pem
Redirect 301 / https://$host_name.example.com/
</VirtualHost>
EOF
fi
done
#
# These comments are skipped.
#
subdomainexmaple1
#
# comments
#
subdomainexmaple1
subdomainexmaple2
subdomainexmaple3
# comment
examplesubdomain1
examplesubdomain2
examplesubdomain3
examplesubdomain4
examplesubdomain5
examplesubdomain6
examplesubdomain7
examplesubdomain8
examplesubdomain9
examplesubdomain10
examplesubdomain11
examplesubdomain12
examplesubdomain13
# blank lines are also skipped
# more comments
examplesubdomain100
examplesubdomain101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment