Add your Dynamic DNS Name to Apache HTACCESS files


stardothosting.com initially created this script for just 1 dynamic IP and can be seen at this URL:

https://www.stackstar.com/blog/2012/05/add-your-dynamic-ips-to-apache-htaccess-files/

I modified their script to meet my needs:

  • Multiple dynamic IPs
  • Allow "allow from xxx.xxx.xxx.xxx" to be inserted into a pre-existing htaccess configuration

To start, you MUST first manually enter the <Files wp-login.php> section into your htaccess file BEFORE first use. If your htaccess file already has this section, then lines 1 & 2 MUST start the section. Lines 3 & 4 can dynamically move down if you have your own wp-login.php configurations that you need to maintain; in other words, add your configuration data between lines 2 & 3. Note: Once you enter this section and save the htaccess file, you WILL NOT BE ABLE to access your Wordpress site until your cron job is first run.

<Files wp-login.php>
order deny,allow
deny from all
</Files>

The cron entry setup is just like stardothosting.com's setup:

*/15 * * * * /bin/sh /usr/local/bin/HTAccessWithMultipleDynamicIPs.sh yourhostname1.dyndns.org /var/www/website.com/public_html/.htaccess > /dev/null 2>&1

You may create multiple cron jobs for multiple dynamic DNS names:

>*/15 * * * * /bin/sh /usr/local/bin/HTAccessWithMultipleDynamicIPs.sh yourhostname2.dyndns.org /var/www/website.com/public_html/.htaccess > /dev/null 2>&1
*/15 * * * * /bin/sh /usr/local/bin/HTAccessWithMultipleDynamicIPs.sh yourhostname3.dyndns.org /var/www/website.com/public_html/.htaccess > /dev/null 2>&1

And so forth...

And now the script:

#!/bin/bash
# Dynamic IP .htaccess file generator
# Initially written by Star Dot Hosting
# https://www.stackstar.com/blog/2012/05/add-your-dynamic-ips-to-apache-htaccess-files/
#
# Added support for multiple Dynamic domains
# Added support to insert the domain into an already existing htaccess file
# by Rob Stokes
# www.assuredsolutions.com
#
# The htaccess file MUST contain this "Files wp-login.php" section upon first use.
# If the sections exists exactly as printed, then insert these lines manually withOUT the pound signs in your htaccess file.
# Once you enter this section, you WILL NOT BE ABLE to access your Wordpress site until your cron job first runs.
#<Files wp-login.php>
#order deny,allow
#deny from all
#</Files>

dynDomain="$1"
htaccessLoc="$2"

dirname="${htaccessLoc%/*}" # get directory/path name
echo "Directory of htaccess file: $dirname"
 
dyn_IP=$(dig +short $dynDomain) # get IP number of $dynDomain
echo "dyn_IP: $dyn_IP"

# verify dyn_IP resembles an IP
if ! echo -n $dyn_IP | grep -Eq "[0-9.]+"; then
    exit 1
fi

# create IP-Hold file name
# https://stackoverflow.com/questions/9697268/how-to-use-regex-to-reverse-text-in-bash-script
domain_r=`echo $dynDomain | awk '{n=split($0,A,".");S=A[n];{for(i=n-1;i>0;i--)S=S"."A[i]}}END{print S}'`
domain_r="$domain_r.txt"
echo "IP-Hold file name: $domain_r"
dyn_file_contents="#allow from $dynDomain=$dyn_IP\nallow from $dyn_IP"

echo "Checking for $dynDomain in $htaccessLoc"
if ! cat $htaccessLoc | grep -q "#allow from $dynDomain"; then

		echo "$dynDomain is NOT in $htaccessLoc"
		# insert $dynDomain in <Files wp-login.php>
		search_s="<Files wp-login.php>\norder deny,allow\n"
		replace_s="<Files wp-login.php>\norder deny,allow\n$dyn_file_contents\n"
		perl -i -p0e "s/$search_s/$replace_s/s" $htaccessLoc

		# save a the new domain name and IP to $domain_r for future reference
		printf "$dyn_file_contents" | tee $dirname/$domain_r
		echo ""

fi

echo "Checking for $dyn_IP in $htaccessLoc"
if ! cat $htaccessLoc | grep -q "$dyn_IP"; then

		echo "$dyn_IP is NOT in $htaccessLoc"
        # grab the old IP
        old_file_contents=`cat $dirname/$domain_r`
        echo "old_file_contents: $old_file_contents"
  		
        # output .htaccess file
		perl -i -p0e "s/$old_file_contents/$dyn_file_contents/s" $htaccessLoc
 
        # save the new ip to remove next time it changes, overwriting previous old IP
		printf "$dyn_file_contents" | tee $dirname/$domain_r
		echo ""

fi