问题现象是,使用nginx作为前端,php-fpm或者apache2后端,开首页收到“Redirection loop”错误,但后台登录正常的问题。这个问题相当典型,答案很多,可以乱搜,但基本上靠谱的方案就是一个:“remove_filter(‘template_redirect’, ‘redirect_canonical’);”。
这里着重要记录的,不是方案本身,只是提供一个脚本,目的在于一次性地修正wp-contents/themes/下的全部主题的functions.php,只要在安装了、更新了主题之后在ssh bash环境中执行一遍该脚本,就会一次性地解决全部问题。
脚本如下:
#!/bin/bash # /var/www/sites/blog.hedzr.com/wp-contents/themes/fix-redirect-loop.sh fix-redirect-loop () { dn=$1 && shift echo "- $dn" if [ "$dn" != "." ]; then if [ -f "$dn/functions.php" ]; then grep -Pni "remove_filter\('template_redirect', 'redirect_canonical'\);" $dn/functions.php >/dev/null || { echo ' not yet, sed...' perl -0777 -i.orig -pe "s/< \?php/一般来说,放这个文件到你的themes目录中,例如:/var/www/sites/blog.hedzr.com/wp-contents/themes/fix-redirect-loop.sh,使用时则这样:
cd /var/www/sites/blog.hedzr.com/wp-contents/themes chmod a+x ./fix-redirect-loop.sh # 这一句执行一次就够了 ./fix-redirect-loop.sh对于没有SSH权利的使用者,只能通过Wordpress后台的主题编辑功能来解决问题了。