|
| 1 | +#!/bin/bash |
| 2 | +# info: Adding force redirect to domain |
| 3 | +# options: USER DOMAIN REDIRECT HTTPCODE [RESTART] |
| 4 | +# labels: hestia web |
| 5 | +# |
| 6 | +# example: v-add-web-domain-redirect user domain.tld domain.tld |
| 7 | +# example: v-add-web-domain-redirect user domain.tld www.domain.tld |
| 8 | +# example: v-add-web-domain-redirect user domain.tld shop.domain.tld |
| 9 | +# example: v-add-web-domain-redirect user domain.tld different-domain.com |
| 10 | +# example: v-add-web-domain-redirect user domain.tld shop.different-domain.com |
| 11 | +# example: v-add-web-domain-redirect user domain.tld different-domain.com 302 |
| 12 | +# |
| 13 | +# Function creates a forced redirect to a domain |
| 14 | + |
| 15 | + |
| 16 | +#----------------------------------------------------------# |
| 17 | +# Variable&Function # |
| 18 | +#----------------------------------------------------------# |
| 19 | + |
| 20 | +# Argument definition |
| 21 | +user=$1 |
| 22 | +domain=$2 |
| 23 | +redirect=$(echo $3 | idn); |
| 24 | +code=${4-301} |
| 25 | +restart=${5-no} |
| 26 | + |
| 27 | +# Includes |
| 28 | +source $HESTIA/func/main.sh |
| 29 | +source $HESTIA/conf/hestia.conf |
| 30 | + |
| 31 | +#----------------------------------------------------------# |
| 32 | +# Verifications # |
| 33 | +#----------------------------------------------------------# |
| 34 | + |
| 35 | +check_args '3' "$#" 'USER DOMAIN REDIRECT [HTTP-CODE] [RESTART]' |
| 36 | +is_format_valid 'user' 'domain' |
| 37 | +is_number_format_valid "$code" "code" |
| 38 | +is_object_valid 'user' 'USER' "$user" |
| 39 | +is_object_unsuspended 'user' 'USER' "$user" |
| 40 | +is_object_valid 'web' 'DOMAIN' "$domain" |
| 41 | +is_object_unsuspended 'web' 'DOMAIN' "$domain" |
| 42 | + |
| 43 | + |
| 44 | +scheme=0 |
| 45 | +if [[ "$3" =~ http://|https:// ]]; then |
| 46 | + scheme=1 |
| 47 | + regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]' |
| 48 | + if ! [[ "$3" =~ $regex ]]; then |
| 49 | + echo "Invalid redirect" |
| 50 | + exit 2; |
| 51 | + fi |
| 52 | +else |
| 53 | + regex='[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]' |
| 54 | + if ! [[ "$3" =~ $regex ]]; then |
| 55 | + echo "Invalid redirect" |
| 56 | + exit 2; |
| 57 | + fi |
| 58 | +fi |
| 59 | + |
| 60 | +# Perform verification if read-only mode is enabled |
| 61 | +check_hestia_demo_mode |
| 62 | + |
| 63 | + |
| 64 | +#----------------------------------------------------------# |
| 65 | +# Action # |
| 66 | +#----------------------------------------------------------# |
| 67 | + |
| 68 | +# Check if proxy is active |
| 69 | +if [ "$WEB_SYSTEM" = 'nginx' ] || [ "$PROXY_SYSTEM" = 'nginx' ]; then |
| 70 | + conf="$HOMEDIR/$user/conf/web/$domain/nginx.conf_redirect" |
| 71 | + sconf="$HOMEDIR/$user/conf/web/$domain/nginx.ssl.conf_redirect" |
| 72 | +fi |
| 73 | +# Insert redirect commands |
| 74 | +if [ ! -z "$PROXY_SYSTEM" ] || [ "$WEB_SYSTEM" = 'nginx' ]; then |
| 75 | + if [ "$scheme" = 1 ]; then |
| 76 | + echo " return $code $redirect\$request_uri;" > $conf |
| 77 | + if [ ! -e "$sconf" ]; then |
| 78 | + ln -s "$conf" "$sconf" |
| 79 | + fi |
| 80 | + else |
| 81 | + echo "if (\$host != \"$redirect\") {" > $conf |
| 82 | + echo " return $code \$scheme://$redirect\$request_uri;" >> $conf |
| 83 | + echo "}" >> $conf |
| 84 | + |
| 85 | + if [ ! -e "$sconf" ]; then |
| 86 | + ln -s "$conf" "$sconf" |
| 87 | + fi |
| 88 | + fi |
| 89 | +else |
| 90 | + echo "Non supported please use .htaccess instead" |
| 91 | + exit 2; |
| 92 | +fi |
| 93 | + |
| 94 | +#----------------------------------------------------------# |
| 95 | +# Hestia # |
| 96 | +#----------------------------------------------------------# |
| 97 | + |
| 98 | +if [ -z "$REDIRECT" ]; then |
| 99 | + add_object_key "web" 'DOMAIN' "$domain" 'REDIRECT' 'U_DISK' |
| 100 | + add_object_key "web" 'DOMAIN' "$domain" 'REDIRECT_CODE' 'U_DISK' |
| 101 | +fi |
| 102 | + |
| 103 | +update_object_value 'web' 'DOMAIN' "$domain" '$REDIRECT' "$redirect" |
| 104 | +update_object_value 'web' 'DOMAIN' "$domain" '$REDIRECT_CODE' "$code" |
| 105 | + |
| 106 | +if [ "$restart" = "yes" ]; then |
| 107 | + # Restarting web server |
| 108 | + $BIN/v-restart-web $restart |
| 109 | + check_result $? "Web restart failed" >/dev/null |
| 110 | + |
| 111 | + $BIN/v-restart-proxy $restart |
| 112 | + check_result $? "Proxy restart failed" >/dev/null |
| 113 | +fi |
| 114 | + |
| 115 | +# Logging |
| 116 | +log_history "Enable forced redirect $domain" |
| 117 | +log_event "$OK" "$ARGUMENTS" |
| 118 | + |
| 119 | +exit |
0 commit comments