|
| 1 | +#!/bin/bash |
| 2 | +# info: Adding force redirect to domain |
| 3 | +# options: USER DOMAIN REDIRECT [CUSTOM] |
| 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 | +# |
| 12 | +# Function creates a forced redirect to a domain |
| 13 | + |
| 14 | + |
| 15 | +#----------------------------------------------------------# |
| 16 | +# Variable&Function # |
| 17 | +#----------------------------------------------------------# |
| 18 | + |
| 19 | +# Argument definition |
| 20 | +user=$1 |
| 21 | +domain=$2 |
| 22 | +redirect=$3 |
| 23 | + |
| 24 | + |
| 25 | +# Includes |
| 26 | +source $HESTIA/func/main.sh |
| 27 | +source $HESTIA/conf/hestia.conf |
| 28 | + |
| 29 | + |
| 30 | +#----------------------------------------------------------# |
| 31 | +# Verifications # |
| 32 | +#----------------------------------------------------------# |
| 33 | + |
| 34 | +check_args '3' "$#" 'USER DOMAIN REDIRECT' |
| 35 | +is_format_valid 'user' 'domain' |
| 36 | +is_object_valid 'user' 'USER' "$user" |
| 37 | +is_object_unsuspended 'user' 'USER' "$user" |
| 38 | +is_object_valid 'web' 'DOMAIN' "$domain" |
| 39 | +is_object_unsuspended 'web' 'DOMAIN' "$domain" |
| 40 | + |
| 41 | +# Perform verification if read-only mode is enabled |
| 42 | +check_hestia_demo_mode |
| 43 | + |
| 44 | + |
| 45 | +#----------------------------------------------------------# |
| 46 | +# Action # |
| 47 | +#----------------------------------------------------------# |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +# Check if proxy is active |
| 52 | +if [ "$WEB_SYSTEM" = 'nginx' ] || [ "$PROXY_SYSTEM" = 'nginx' ]; then |
| 53 | + conf="$HOMEDIR/$user/conf/web/$domain/nginx.conf_redirect" |
| 54 | + sconf="$HOMEDIR/$user/conf/web/$domain/nginx.ssl.conf_redirect" |
| 55 | +fi |
| 56 | +# Insert redirect commands |
| 57 | +if [ ! -z "$PROXY_SYSTEM" ] || [ "$WEB_SYSTEM" = 'nginx' ]; then |
| 58 | + echo "if (\$host != \"$redirect\") {" > $conf |
| 59 | + echo " return 301 \$scheme://$redirect\$request_uri;" >> $conf |
| 60 | + echo "}" >> $conf |
| 61 | + |
| 62 | + if [ ! -e "$sconf" ]; then |
| 63 | + ln -s "$conf" "$sconf" |
| 64 | + fi |
| 65 | +else |
| 66 | + echo "Non supported please use .htaccess instead" |
| 67 | + exit 2; |
| 68 | +fi |
| 69 | + |
| 70 | +#----------------------------------------------------------# |
| 71 | +# Hestia # |
| 72 | +#----------------------------------------------------------# |
| 73 | + |
| 74 | +if [ -z "$REDIRECT" ]; then |
| 75 | + add_object_key "web" 'DOMAIN' "$domain" 'REDIRECT' 'TIME' |
| 76 | +fi |
| 77 | + |
| 78 | +update_object_value 'web' 'DOMAIN' "$domain" '$REDIRECT' "$redirect" |
| 79 | + |
| 80 | +# Restart web server |
| 81 | +$BIN/v-restart-web |
| 82 | +check_result $? "Web restart failed" > /dev/null |
| 83 | + |
| 84 | +# Logging |
| 85 | +log_history "Enable forced redirect $domain" |
| 86 | +log_event "$OK" "$ARGUMENTS" |
| 87 | + |
| 88 | +exit |
0 commit comments