WP forwarding to wrong site address

You may have some problem with JavaScript on your website, with the hardcoded URLs. As I can see you are trying to create the test environment.

This is good, and I like people creating the test environments.
You need to go to through your code, JavaScript and PHP to seek for the hardcoded URLs.

Also it is good to have the lines like this in your wp-config.php:

define( 'WP_SITEURL', 'http://example.com.mytestdomain.com' );
define( 'WP_HOME', 'http://example.com.mytestdomain.com' );

I would suggest you do the database export something like

wp db export

If you use wp-cli
and after that try to search the export to understand what you have in there. Let us know the feedback.

Anyway, very interesting question.
Please feel free to accept this answer if you find it helpful.
I think the approach explained in here is really general but you may find it helpful.


From the web server configuration standpoint, try to create a simple index.php in a root of your WordPress that will print “You cannot redirect me!”. Let me know the feedback if this will be redirected. 😉

PS: Don’t forget to backup the original WordPress’s index.php.


So, I think I have a better idea of what is happening. Although the site’s address is set correctly to the test site, and even the pages show the correct test site permalinks, all posts still contain, hard coded in the DB, a reference to the original site. Since the site, after calling index.php, immediately opens post number 1, it is being redirected to the original site. Frankly, this is strange, I thought the purpose of the site url and home url is to prevent just that? I will now try changing wp-config.php to see if it has a different effect than setting homeurl and siteurl in the db

You can do something like this once:

update_option( 'siteurl', 'http://example.com.mytestdomain.com' );
update_option( 'home', 'http://example.com.mytestdomain.com' );

After that check what you have in the database when you export it.

Also note, some menu links may be hardcoded in the database, in the options table.