redirection manager

Create, list, remove Nginx redirections.
This commit is contained in:
Cristhian Martínez Ochoa 2019-12-26 21:56:50 -07:00
parent a938f5d54e
commit 8be5259be6
2 changed files with 137 additions and 32 deletions

View file

@ -718,3 +718,100 @@ force_redirect() {
sudo rm /tmp/template-site-wwwredirect
}
redirection_manager() {
if [[ -n $list ]]; then
echo "${gre}"
sudo sed -n '/RedirectFrom/{h;d;}; H; /To:/{x;p;}' /etc/nginx/apps.d/a.com-nginx.conf | sed '/# To:/ s/$/\n/' | sed 's/^# //'
echo "${end}"
elif [[ -n $delete ]]; then
echo ""
[[ -z $from ]] && read -p "${blu}Redirect 'from path' to delete: ${end}" from
if [[ -z $from ]]; then
echo "${red}[ERROR] Please, enter a valid 'from' path!${end}"
exit 1
fi
if [[ -a /etc/nginx/apps.d/$domain-nginx.conf ]]; then
sudo sed -i "/^# RedirectFrom: $(echo $from | sed "s#/#\\\/#g")/,/^# RedirectEnd/{/.*/d}" /etc/nginx/apps.d/$domain-nginx.conf
if [[ ! -s /etc/nginx/apps.d/$domain-nginx.conf ]]; then
sudo rm /etc/nginx/apps.d/$domain-nginx.conf
sudo sed -i "/$domain-nginx.conf;/d" /etc/nginx/sites-available/$domain
fi
fi
echo "${gre}Redirection successfully removed!${end}"
else
# Default type
[[ -z $http_code ]] && http_code="302"
echo ""
[[ -z $from ]] && read -p "${blu}Redirect from: ${end}" from
[[ -z $to ]] && ! [[ $http_code =~ ^(410|444|451)$ ]] && read -p "${blu}Redirect to: ${end}" to
echo ""
if [[ -z $from || ( -z $to && $http_code =~ ^(301|302|303|307|308)$ ) ]]; then
echo "${red}[ERROR] Please, enter a valid from/to redirection path!${end}"
exit 1
elif ! [[ $http_code =~ ^(301|302|303|307|308|410|444|451)$ ]]; then
echo "${red}[ERROR] Invalid redirection HTTP Code!${end}"
exit 1
elif ! [[ $from =~ ^\/([A-Za-z0-9_\/\.\-]+)?$ ]]; then
echo "${red}[ERROR] Please, enter a valid from/to redirection path!${end}"
exit 1
elif [[ -n $to ]] && ! [[ $to =~ ^\/([A-Za-z0-9_\/\.\-]+)?$ ]]; then
echo "${red}[ERROR] Please, enter a valid from/to redirection path!${end}"
exit 1
fi
if [[ ! -a /etc/nginx/apps.d/$domain-nginx.conf ]]; then
sudo touch /etc/nginx/apps.d/$domain-nginx.conf
sudo chmod 644 /etc/nginx/apps.d/$domain-nginx.conf
sudo chown -R root:root /etc/nginx/apps.d/$domain-nginx.conf
fi
exist=$( grep -E "^# RedirectFrom: ${from}$" /etc/nginx/apps.d/$domain-nginx.conf )
if [[ -z $exist ]]; then
if [[ $exact == true ]]; then
sign="= "
code="$from (Exact)"
else
sign=""
code="$from"
fi
if [[ $http_code =~ ^(301|302|303|307|308)$ ]]; then
echo "# RedirectFrom: ${code}
# To: $to ($http_code)
location $sign$from {
return $http_code $to;
}
# RedirectEnd" >> /etc/nginx/apps.d/$domain-nginx.conf
elif [[ $http_code =~ ^(410|451)$ && -n $to ]]; then
echo "# RedirectFrom: ${code}
# To: $to ($http_code)
location $sign$from {
error_page $http_code $to;
return $http_code;
}
# RedirectEnd" >> /etc/nginx/apps.d/$domain-nginx.conf
elif [[ $http_code =~ ^(410|444|451)$ && -z $to ]]; then
echo "# RedirectFrom: ${code}
# To: $to ($http_code)
location $sign$from {
return $http_code;
}
# RedirectEnd" >> /etc/nginx/apps.d/$domain-nginx.conf
fi
vconf=$( grep -E "include apps.d/$domain-nginx.conf;" /etc/nginx/sites-available/$domain )
[[ -z $vconf ]] && sudo sed -i "/# WebinolyCustom$/a \ include apps.d/$domain-nginx.conf;" /etc/nginx/sites-available/$domain
echo "${gre}Redirection successfully added!${end}"
else
echo "${gre}Redirection already exists!${end}"
fi
fi
}

View file

@ -122,40 +122,9 @@ fi
[[ $type == [24] ]] && check_for_mysql -ask
# List Sites
if [[ -n $list ]]; then
echo ""
for site in "/etc/nginx/sites-available"/*
do
domi=$(echo $site | cut -f 5 -d "/")
if [[ $domi != "default" && $domi != $(conf_read tools-port) ]]; then
[[ -a /var/www/$domi ]] && sign="${gre} +" || sign="${blu} *${gre}"
[[ ! -L /etc/nginx/sites-enabled/$domi ]] && sign="${red} -"
outlist="$sign $domi ${end}"
if [[ $list == "raw" || $raw =~ ^(all|parked|disabled|main|true)$ ]]; then
outlist="$domi"
[[ $raw == "parked" && -a /var/www/$domi ]] && outlist=""
[[ $raw == "disabled" && -L /etc/nginx/sites-enabled/$domi ]] && outlist=""
[[ $raw == "main" && ( ! -L /etc/nginx/sites-enabled/$domi || ! -a /var/www/$domi ) ]] && outlist=""
elif [[ -n $raw ]]; then
echo "${red}[ERROR] Please, enter a valid value for RAW!${end}"
exit 1
fi
if [[ -n $outlist ]]; then
echo "$outlist"
nonemptylist=true
fi
fi
done
[[ -z $nonemptylist && -z $raw && $list != "raw" ]] && echo "${blu}[Empty] No sites were found!${end}"
echo ""
nginx_not="true" # Nginx-Reload not-needed.
# Delete all sites
elif [[ -n $delete_all ]]; then
if [[ -n $delete_all ]]; then
api-events_update si7
echo "${red}"
echo "All your sites files will be removed."
@ -478,6 +447,12 @@ elif [[ -n $on && ! -L /etc/nginx/sites-enabled/$domain && -a /etc/nginx/sites-a
sudo ln -s /etc/nginx/sites-available/$domain /etc/nginx/sites-enabled/$domain
echo "${gre}Site${blu} $domain ${gre}has been successfully enabled!${end}"
# Redirection Manager
elif [[ -n $redirection && ( -f /etc/nginx/sites-available/$domain || -n $list ) ]]; then
check_for_nginx
redirection_manager
# Delete site
elif [[ -n $delete && -a /etc/nginx/sites-available/$domain ]]; then
@ -741,6 +716,7 @@ _EOF_
echo "${gre}Site ${blu}'$domain$subfolder'${gre} is now a successful clone of ${blu}'$clone_from$subfolder'${end}"
# Replace Content
elif [[ -n $replace_content && -a /etc/nginx/sites-available/$domain ]]; then
if [[ $(is_wp $domain $subfolder) != "true" ]]; then
echo "${red}[ERROR] Site${blu} $domain ${red}is not a WordPress site!${end}"
@ -775,6 +751,38 @@ elif [[ -n $replace_content && -a /etc/nginx/sites-available/$domain ]]; then
echo "${gre}Content successfully replaced in your WP site!${end}"
# List Sites
elif [[ -n $list ]]; then
echo ""
for site in "/etc/nginx/sites-available"/*
do
domi=$(echo $site | cut -f 5 -d "/")
if [[ $domi != "default" && $domi != $(conf_read tools-port) ]]; then
[[ -a /var/www/$domi ]] && sign="${gre} +" || sign="${blu} *${gre}"
[[ ! -L /etc/nginx/sites-enabled/$domi ]] && sign="${red} -"
outlist="$sign $domi ${end}"
if [[ $list == "raw" || $raw =~ ^(all|parked|disabled|main|true)$ ]]; then
outlist="$domi"
[[ $raw == "parked" && -a /var/www/$domi ]] && outlist=""
[[ $raw == "disabled" && -L /etc/nginx/sites-enabled/$domi ]] && outlist=""
[[ $raw == "main" && ( ! -L /etc/nginx/sites-enabled/$domi || ! -a /var/www/$domi ) ]] && outlist=""
elif [[ -n $raw ]]; then
echo "${red}[ERROR] Please, enter a valid value for RAW!${end}"
exit 1
fi
if [[ -n $outlist ]]; then
echo "$outlist"
nonemptylist=true
fi
fi
done
[[ -z $nonemptylist && -z $raw && $list != "raw" ]] && echo "${blu}[Empty] No sites were found!${end}"
echo ""
nginx_not="true" # Nginx-Reload not-needed.
# Catch ERROR
elif [[ -n $domain && ! -a /etc/nginx/sites-available/$domain ]]; then
echo "${red}[ERROR] Site${blu} $domain ${red}doesn't exists, please enter a valid domain name!${end}"