4

I'm working on some edits for a client's site. So I downloaded the site's files and exported the database.

I need to make some big changes on a development machine first so I won't mess with the live site.

Then I imported the database to my development machine and setup the files as well. But whenever I go to http://localhost it redirects me to the client's site: https://theclientsite.com

So since it redirects me to a https address I'm thinking it probably has something to do with that.

I did a global search on the code for the client's domain, but I don't see anything that redirects.

What could be the source of the redirecting? If the site doesn't stop redirecting, I can't work.

  • Is there any evidence that this redirect is WordPress related? – Chip Bennett May 9 '12 at 20:07
  • I asked my client if he installed any plugin that would do that and he said no. – leonel May 9 '12 at 20:10
  • there is no .htaccess file either – leonel May 9 '12 at 20:10
3

Usually, this is a problem with the Site URL or WordPress URL not being set properly. If you just copied the database, WordPress itself will try to redirect the domain back to what it thinks is the right site.

You have two options:

1. Edit the Settings

Use PHPMyAdmin or a similar tool to edit the site url and WordPress url settings in the database. This should fix the problem assuming nothing else on your development box is forcing the redirect.

2. Change your Hosts File

This is the better solution. Edit your hosts file to point http://clientsite.com at your local IP address (127.0.0.1). Then, just navigate to the development site the same way you would the production site. Just remember to change your hosts file back when you're done developing.

  • just edited the database for both the siteurl and home options. i'm going to try the hosts file solution now – leonel May 9 '12 at 20:26
  • ok, i edited my hosts file and it did work, the site is not redirected anymore, but now the links don't work, i can only see the homepage. i know it has something to do with permalinks because i go to domain.com/tour and i get a 404 not found error – leonel May 9 '12 at 20:59
  • 1
    @leonel Sounds like you're missing your .htaccess file. Make sure it's in the site's root, and that it has the "pretty permalink" rewrite rules in it. – MathSmath May 9 '12 at 21:57
  • 1
    @MathSmath Editing your hosts file is one of the most often recommended solutions due to the way WP serializes data containing the site URL. Knowing which version you're on (production vs development) is a user issue. – EAMann May 9 '12 at 23:16
  • 1
    @EAMann I know it's a common practice, but IMO it leaves a lot of room for developer error, and is a but of hassle to keep up with. An alternative that I find works perfectly (and avoids the serialization issue) is to use URLs of the same length for each deployment context. For example, use www.dot for production, stg.dot for staging and loc.dot for local. Then, when you find/replace the domain, serialization isn't an issue. Of course, everybody has their own way of doing things, but this has worked really well for me in cases where I'm not using a subdomain. YMMV. Either way, good answer! – MathSmath May 10 '12 at 19:20
2

I concur with EAMann that it is probably database-related. I, however, think the first solution is better than the second of the two provided. When I move WordPress installations (usually from development to production rather than the opposite, like you), I simply run a few UPDATE statements in phpMyAdmin. This is what I run:

update wp_posts set post_content = replace(post_content,'development.org','production.org');
update wp_posts set guid = replace(guid,'development.org','production.org');
update wp_options set option_value = replace(option_value,'development.org','production.org');
update wp_commentmeta set meta_value = replace(meta_value,'development.org','production.org');

Make sure you edit the URLs and table names according to your situation.

  • 1
    It's annoying, but this is the correct answer; it's best to update all these table fields. I'd suggest making a script to do this. – axlotl Feb 9 '18 at 23:10
  • 1
    Just be careful when running a search-replace on the database of a wordpress site. Many fields are serialized, which means that the number of characters in those fields is stored with the data. If you change the number of characters in a field, without also changing the indicated number of characters for that field, then that data is considered corrupt, and can break parts of your site. So just make sure none of the fields to be updated contain serialization data. Or use a plugin that handles serialized data. The wp-cli search-replace command line tool also handles serialization. – SherylHohman Jun 14 at 0:36
  • I never had problems with this, but sure. A comment above also pointed out the same thing back in 2012; along with a solution: have the same number of characters in development and production hostnames :) – Jobjörn Folkesson Jun 16 at 16:10
0

If you're into wp-cli.org this will do the trick in many cases:

wp search-replace example.com dev.example.com

Or if you're on localhost:

wp search-replace example.com localhost

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.