Finding what is causing my site to issue 301 redirects
Any of the following can cause redirects:
.htaccess
files (cPanel usually edits one of these files)- Apache configuration files (usually
httpd.conf
) - In code (such as a plugin) that sets a
Location:
header.
To locate the source of redirects, I usually search all the source code using recursive grep
on the command line. I usually search for the URL of the redirect.
URLs can be stored in the database as well (some WordPress plugins might do that), so it would be good to look there as well.
If it is malware that hacked your site and caused the redirect, the URL may be obfuscated in the source code to make finding it harder.
I just had this happen, took me a while to find the setting but if you are using Plesk then check this setting from Plesk Panel.
Navigate to Home > Subscriptions > yourdomain.com > Websites & Domains > yourdomain.com > Hosting Settings. Then check "Preferred domain" dropdown you can choose www.
or domain.
If you are using CloudFlare purge cache and switch to development mode.
Some cache plugins can do redirects as well. Better empty the cache directory.
Theme plugins might have some weird issues as well.
Making a backup and deleting plugin directories one by one can help to find bad apples easily. (In case the logs aren't helpful.)
Maybe this saves some hours for someone.