Skip to content

Commit

Permalink
force redirect
Browse files Browse the repository at this point in the history
Force redirect issue, not updating WP db when installed in subfolder, or is multisite.
  • Loading branch information
QROkes committed Sep 7, 2022
1 parent feb642d commit cc3ec53
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
51 changes: 36 additions & 15 deletions lib/general
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,8 @@ wp_conf_retrieve() {


# In case of parked sites with WP domain mapping
if [[ $3 != "false" && $(is_parked $1) == "true" ]]; then
#if [[ $3 != "false" && $(is_parked $1) == "true" ]]; then # Modified recently, not sure the impact!
if [[ $3 != "false" ]]; then
# If domain doesn't exist, check if it's a subsite of a subdomain Multisite network.
if [[ ! -f /etc/nginx/sites-available/$1 && -f /etc/nginx/sites-available/$(echo $1 | cut -d "." -f 2- -s) ]]; then
local maindom=$(echo $1 | cut -d "." -f 2- -s)
Expand All @@ -531,6 +532,8 @@ wp_conf_retrieve() {
if [[ $wp_blogid =~ ^[0-9]+$ && $wp_blogid -gt 1 ]]; then
wp_dbpref="${wp_dbpref}${wp_blogid}_"
echo "${blu}${dim}Site${end}${dim} ${1}${4} ${blu}is a subsite (${wp_dbpref}) in a WP Multisite Network!${end}" >&2
elif [[ $wp_blogid == 1 ]]; then
echo "${blu}${dim}Site${end}${dim} ${1}${4} ${blu}is the main site in a WP Multisite Network!${end}" >&2
else
[[ $wp_blogid != 1 ]] && wp_blogid=""
fi
Expand Down Expand Up @@ -1057,23 +1060,26 @@ email_update() {
edit_wp_db_url_multisite() {
#Subfolder is not allowed here because is not needed, parked and force-www are not allowed in subfolders.
#Example: edit_wp_db_url_multisite olddomain.com newdomain.com 2
# $3 - WP BlogID to force
# $3 - WP BlogID to force (optional)

if [[ -n $1 && -n $2 && -n $3 && $3 =~ ^[0-9]+$ && $(is_wp_multisite $1) =~ ^(subdomain|subdirectory)$ ]]; then
wp_conf_retrieve $1 true false $subfolder
if [[ -n $1 && -n $2 && $(is_wp_multisite $1) =~ ^(subdomain|subdirectory)$ ]]; then
wp_conf_retrieve $1 true true $subfolder

# Force WP blogID
local wp_dbpref="${wp_dbpref}${3}_"
local wp_blogid=$3
local dbsetup="SELECT * FROM information_schema.tables WHERE table_schema = '$wp_dbname' AND table_name = '${wp_dbpref}options' LIMIT 1;"
if [[ $wp_dbhost == "localhost" && -n $(sudo mysql --connect-timeout=10 --user=admin -p$ADMIN_PASS -e "$dbsetup") ]]; then
echo "${blu}${dim}WordPress blog ID (${wp_dbpref}) found and validated in a WP Multisite Network!${end}" >&2
elif [[ $wp_dbhost != "localhost" && -n $(sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" -e "$dbsetup") ]]; then
echo "${blu}${dim}WordPress blog ID (${wp_dbpref}) found and validated in a WP Multisite Network!${end}" >&2
else
echo "${red}${dim}[ERROR] WordPress blog ID (${wp_dbpref}) not found!${end}" >&2
wp_dbpref=""
wp_blogid=""
if [[ -n $3 && $3 =~ ^[0-9]+$ ]]; then
local wp_dbpref="${wp_dbpref}${3}_"
local wp_blogid=$3
local dbsetup="SELECT * FROM information_schema.tables WHERE table_schema = '$wp_dbname' AND table_name = '${wp_dbpref}options' LIMIT 1;"

if [[ $wp_dbhost == "localhost" && -n $(sudo mysql --connect-timeout=10 --user=admin -p$ADMIN_PASS -e "$dbsetup") ]]; then
echo "${blu}${dim}WordPress blog ID (${wp_dbpref}) found and validated in a WP Multisite Network!${end}" >&2
elif [[ $wp_dbhost != "localhost" && -n $(sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" -e "$dbsetup") ]]; then
echo "${blu}${dim}WordPress blog ID (${wp_dbpref}) found and validated in a WP Multisite Network!${end}" >&2
else
echo "${red}${dim}[ERROR] WordPress blog ID (${wp_dbpref}) not found!${end}" >&2
wp_dbpref=""
wp_blogid=""
fi
fi

if [[ -n $wp_dbhost && -n $wp_dbname && -n $wp_dbpref_main && -n $wp_blogid ]]; then
Expand Down Expand Up @@ -1139,6 +1145,21 @@ _EOF_
}


wp_db_update() {
# Same as edit_wp_db_url, but it also check and includes WP in subfolders.

# if main site is WP.
[[ $(is_wp $1) == "true" ]] && edit_wp_db_url $1 $2

# Check if site contains WP in subfolders.
for site in "/etc/nginx/apps.d/${1}_"*-wpcommon.conf
do
local subwp="/$(echo $site | cut -f 2- -d "_" -s | cut -f -1 -d "-" -s | sed "s/_/\//g")"
[[ -n $subwp && -f /var/www/${1}/htdocs$subwp/wp-config.php ]] && edit_wp_db_url $1 ${2}${subwp} $subwp
done
}


db_role_check() {
if ! [[ $1 =~ ^(basic|limited|extra|complete|full|all|grant)$ ]]; then
echo "${red}[ERROR] Please, enter a valid database role!${end}"
Expand Down
24 changes: 6 additions & 18 deletions lib/site-ssl
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@
# Functions for SSL On/Off


ssl_wp_db_update() {
# if main site is WP.
[[ $(is_wp $domain) == "true" ]] && edit_wp_db_url $domain $1

# Check if site contains WP in subfolders.
for site in "/etc/nginx/apps.d/${domain}_"*-wpcommon.conf
do
local subwp="/$(echo $site | cut -f 2- -d "_" -s | cut -f -1 -d "-" -s | sed "s/_/\//g")"
[[ -n $subwp && -f /var/www/$domain/htdocs$subwp/wp-config.php ]] && edit_wp_db_url $domain ${1}${subwp} $subwp
done
}

ssl_nginx() {
sudo sed -i '/listen 80/c \ listen 443 ssl http2;' /etc/nginx/sites-available/$domain
sudo sed -i '/listen \[::\]:80/c \ listen [::]:443 ssl http2;' /etc/nginx/sites-available/$domain
Expand Down Expand Up @@ -48,9 +36,9 @@ ssl_nginx() {
# Not duplicate: Custom cert function does not run the same way as LE requests.
# For some unknown reason this condition doesn't work in oneline as we usually do!
if [[ $(is_force_redirect $domain) == "www" ]]; then
ssl_wp_db_update "https://www.${domain}"
wp_db_update $domain "https://www.${domain}"
else
ssl_wp_db_update "https://${domain}"
wp_db_update $domain "https://${domain}"
fi

if [[ -n $ssl_ocsp && -f $ssl_ocsp ]]; then
Expand Down Expand Up @@ -190,9 +178,9 @@ site_ssl_on() {

# For some unknown reason this condition doesn't work in oneline as we usually do!
if [[ $(is_force_redirect $domain) == "www" ]]; then
ssl_wp_db_update "https://www.${domain}"
wp_db_update $domain "https://www.${domain}"
else
ssl_wp_db_update "https://${domain}"
wp_db_update $domain "https://${domain}"
fi

echo "${gre}SSL have been successfully enabled for your site${blu} $domain${end}"
Expand Down Expand Up @@ -270,8 +258,8 @@ site_ssl_off() {
echo "${gre}${dim}Tools Site updated with SSL!${end}"
fi

[[ -z $no_db_update && $(is_force_redirect $domain) != "www" ]] && ssl_wp_db_update "http://${domain}"
[[ -z $no_db_update && $(is_force_redirect $domain) == "www" ]] && ssl_wp_db_update "http://www.${domain}"
[[ -z $no_db_update && $(is_force_redirect $domain) != "www" ]] && wp_db_update $domain "http://${domain}"
[[ -z $no_db_update && $(is_force_redirect $domain) == "www" ]] && wp_db_update $domain "http://www.${domain}"

# Check if exist to not try to revoke a cert that don't exist, for example a site with a cert added to a wildcard! (-add-to-wildcard)
if [[ -f /etc/letsencrypt/renewal/${domain}.conf ]]; then
Expand Down
6 changes: 3 additions & 3 deletions lib/sites
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ force_redirect() {
case $force_redirect in
"www")
[[ -n $rediron ]] && sudo sed -i '/WebinolyWWWredirectStart/,/WebinolyWWWredirectEnd/{/.*/d}' /etc/nginx/sites-available/$domain
edit_wp_db_url $domain "${scheme}://www.${domain}"
wp_db_update $domain "${scheme}://www.${domain}"
edit_wp_db_url_multisite $domain "www.${domain}"

sudo sed -i "/#server_name;/c \ server_name $domain;" /tmp/template-site-wwwredirect
Expand All @@ -814,7 +814,7 @@ force_redirect() {
;;
"root")
[[ -n $rediron ]] && sudo sed -i '/WebinolyWWWredirectStart/,/WebinolyWWWredirectEnd/{/.*/d}' /etc/nginx/sites-available/$domain
edit_wp_db_url $domain "${scheme}://${domain}"
wp_db_update $domain "${scheme}://${domain}"
edit_wp_db_url_multisite $domain $domain

sudo sed -i "/#server_name;/c \ server_name www.$domain;" /tmp/template-site-wwwredirect
Expand All @@ -824,7 +824,7 @@ force_redirect() {
;;
"off")
if [[ -n $rediron ]]; then
edit_wp_db_url $domain "${scheme}://${domain}"
wp_db_update $domain "${scheme}://${domain}"
edit_wp_db_url_multisite $domain $domain

sudo sed -i '/WebinolyWWWredirectStart/,/WebinolyWWWredirectEnd/{/.*/d}' /etc/nginx/sites-available/$domain
Expand Down
2 changes: 1 addition & 1 deletion usr/site
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ elif [[ -n $ssl ]]; then
ssl_nginx

# No need of force-redirect check because this site is always subdomain and subdomains are not supported in force-redirect.
[[ $(is_wp $domain) == "true" ]] && edit_wp_db_url $domain "https://${domain}"
[[ $(is_wp $domain) == "true" ]] && wp_db_update $domain "https://${domain}"

echo "${gre}Your site${blu} $domain ${gre}have been successfully added to the${blu} $add_to_wildcard ${gre}wildcard cert.${end}"
else
Expand Down

0 comments on commit cc3ec53

Please sign in to comment.