Added final check to make sure htaccess is correct

main
Anthony 2025-07-16 23:12:24 +08:00
parent 0dd9bd6af8
commit d56286d5b9
1 changed files with 44 additions and 0 deletions

View File

@ -774,6 +774,50 @@ EOF
fi
success "WordPress installed successfully via WP-CLI."
# ------------------------------------------------------------------
# Final check: ensure .htaccess has the WordPress rewrite directives
# Some plugins (e.g., LiteSpeed Cache) may recreate .htaccess and drop
# the default WordPress block. If it's missing, append it now.
# ------------------------------------------------------------------
if [[ -f ".htaccess" ]] && ! grep -q "# BEGIN WordPress" .htaccess; then
warning ".htaccess is missing WordPress rewrite rules adding them."
# Re-establish the path to the template in case this block is far
# from the earlier variable scope.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HTACCESS_TEMPLATE="$SCRIPT_DIR/templates/htaccess.template"
WORDPRESS_BLOCK=""
if [[ -f "$HTACCESS_TEMPLATE" ]]; then
WORDPRESS_BLOCK=$(awk '/# BEGIN WordPress/{flag=1} flag{print} /# END WordPress/{flag=0}' "$HTACCESS_TEMPLATE")
fi
# Fallback inline snippet if template missing or awk failed
if [[ -z "$WORDPRESS_BLOCK" ]]; then
read -r -d '' WORDPRESS_BLOCK <<'WPBLOCK' || true
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
WPBLOCK
fi
# Append the block to .htaccess
printf "%s\n" "$WORDPRESS_BLOCK" | sudo tee -a .htaccess > /dev/null || warning "Failed to append WordPress rules to .htaccess"
sudo chown "${WEB_USER}:${WEB_GROUP}" .htaccess || warning "Failed to set ownership after appending WordPress rules."
fi
else
info "WordPress is already installed according to WP-CLI."
# Optionally update URL if needed (be careful with this, can break site if proxy/etc involved)