-
Notifications
You must be signed in to change notification settings - Fork 2
/
search-replace-ssl.bash
62 lines (62 loc) · 2.96 KB
/
search-replace-ssl.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
# run like this:
# curl -sO https://raw.githubusercontent.com/renatofrota/search-replace-ssl/master/search-replace-ssl.bash && bash search-replace-ssl.bash
echo -e "\n\tsearch-replace-ssl - v0.0.3 - https://github.com/renatofrota/search-replace-ssl\n";
home=$(wp option get home --skip-plugins --skip-themes | cut -d / -f 3-);
echo "HOME: $home";
wp=$(echo $home | sed 's|^www\.||');
echo "DOMAIN: $wp";
[[ "$home" == "www."* ]] && prefix="www." || prefix="";
read -${BASH_VERSION+e}rp "Search-replace to https? (y/N) " -n 1 sr;
if [[ "$sr" =~ (1|y|Y|s|S) ]]; then
wpr=$(echo ${wp} | cut -d / -f 1);
wp db export ${HOME}/$(date +%F-%H-%M-%S)-${wpr}-backup.sql;
echo "Step 1 - Replacing http://(www.)${wpr} -> https://${prefix}${wpr}";
wp search-replace --precise --recurse-objects --all-tables --regex "https?:\/\/(www\\.)?${wpr}" "https://${prefix}${wpr}" --skip-themes --skip-plugins | grep -w -v "0\|skipped";
wp cache flush --skip-themes --skip-plugins;
echo "Step 2 - Replacing http:\/\/(www.)${wpr} -> https:\/\/${prefix}${wpr}";
wp search-replace --precise --recurse-objects --all-tables "https?:\/\/(www\\.)?${wpr}" "https:\/\/${prefix}${wpr}" --skip-themes --skip-plugins | grep -w -v "0\|skipped";
wp cache flush --skip-themes --skip-plugins;
echo "Step 3 - Replacing http%3A%2F%2F(www.)${wpr} -> https%3A%2F%2F${prefix}${wpr}";
wp search-replace --precise --recurse-objects --all-tables "https?%3A%2F%2F(www\\.)?${wpr}" "https%3A%2F%2F${prefix}${wpr}" --skip-themes --skip-plugins | grep -w -v "0\|skipped";
wp cache flush --skip-themes --skip-plugins;
fi
echo
read -${BASH_VERSION+e}rp "Add redirection rules to .htaccess? (y/N) " -n 1 htrules;
if [[ "$htrules" =~ (1|y|Y|s|S) ]]; then
if [ "${wp}" != "${home}" ]; then
wp1="www.";
wp2="";
wp3="add";
else
wp1="";
wp2="www\.";
wp3="remove";
fi;
wpe=$(echo $wp | sed 's|\.|\\.|g' | sed 's|^www\\.||' | cut -d / -f 1);
content="# BEGIN HTTPS
# force https:// (and ${wp3} www prefix) in a single redirection
# prevent 'chained redirects' reducing 'TTFB' and improving scores
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^(www\.)?${wpe}
RewriteRule ^(.*)$ https://${wp1}${wp}/\$1 [R=301,L]
# ${wp3} www prefix on requests that are sent over HTTPS protocol
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^${wp2}${wpe}
RewriteRule ^(.*)$ https://${wp1}${wp}/\$1 [R=301,L]
# set HTTPS env var on forwarded requests (prevent redirection loop)
SetEnvIf X-Forwarded-Proto https HTTPS=on
# END HTTPS
";
echo "$content" > .htaccess_enablingssl;
cat .htaccess >> .htaccess_enablingssl;
mv .htaccess_enablingssl .htaccess;
echo -e "\nAdded to .htaccess:";
echo "$content";
echo
fi
killme() {
[[ "$0" == "search-replace-ssl.bash" ]] && echo -n "Done! Self destroying... " && sleep 1 && rm -fv "$0" || echo "It's all done. Do not forget to remove this script.";
}
trap killme EXIT