I am just transferring over a WordPress site from Server 1 to Server 2. The IP address of Server 1 points to example.com (A record on DNS server) The IP address of Server 2 points to www.example.com (A record on DNS server)

During the migration, I want to type in www.example.com and prevent any redirection - so that I can make sure that it has been set up correctly before setting the A record for example.com to point to Server 2.

There is a .htaccess file on Server 2, but it isn't redirecting to example.com. Just to make sure that it isn't, I renamed it to htaccess.bk to make sure it doesn't kick in - but no success here.

Any idea what I need to do?


  • Can you share your htaccess code here. Oct 31, 2018 at 10:49

2 Answers 2


What do you have set in the WordPress admin under Settings -> General and then WordPress Address and Site Address? Make sure these are both using the www.example.com domain.

If you want to use an htaccess rule, which will run before WordPress kicks in, there are plenty of answers on StakOverflow such as this one: https://stackoverflow.com/questions/4916222/htaccess-how-to-force-www-in-a-generic-way


Just put this code in your wp-config.php file

define( 'WP_HOME', 'your_url' );
define( 'WP_SITEURL', 'your_url' );

In .htaccess file:

Force www:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

Force non-www:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

Note: You need just comment out these two lines in .htaccess file by using # just putting before the line

RewriteEngine on
#RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
#RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
  • Please explain why do you think it will be helpful... Oct 31, 2018 at 10:33
  • Actually, home_url and site_url exist in the wp_options table so first WordPress checks in that table. We can override it from config file also. Oct 31, 2018 at 10:36
  • I know, but... If you want your answer to be helpful, then always explain such solutions, so anyone reading it may understand how does it work ;) Oct 31, 2018 at 10:38
  • These settings are useful when working on a local or staging server so that you don't have to keep changing the database wp_options. We run a WordPress composer setup with a local-wp-config file include with settings specific to the server environment. In the OP's case, they should just edit the URLs via the admin. Oct 31, 2018 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.