4

I often have to switch what IP a name resolves to in order to do certain tasks when constructing or more specifically moving websites/web apps, to a new platform/server. Many times I have to go back and forth quickly, which mean editing etc/hosts and refreshing a lot, which is annoying.

Is there any program/plugin/browser extension(preferably for Chrome or FF) that allows me to quickly change the IP back and forth, or lets the browser choose its own DNS server or hosts file independent of the system's (so I could just use one browser using the system settings and another using its own settings, eg. Chrome resolves example.com to 1.1.1.1 while FF resolves example.com to 1.2.2.2) ?

I develop on a Mac, so you can provide PC extensions as part of your answer but please make sure that you have at least one solution that works on Mac.

5 Answers 5

2
+50

Firefox addon

  1. SwitchHost (Mozilla/Firefox Addon site).

    Base on the description below, it is 100% OP asking for (switch/using host file)

    SwitchHosts allows you to manage and switch between multiple hosts file quickly and easily...

  2. HostAdmin

  3. TamperData

Both (1) and (2) archive the result by modifying the host file which WILL affect other program.

(3) maybe more to your need if you do not want to affect other program. It also has the advantage of not requiring changing write permission of the /etc/hosts file, which can be problematic (or even no effect) in OS X.

2
  • Do you know if it still works on recent Firefox and OS versions? The comments on the plugin page make me worry.
    – Thilo
    Mar 1, 2013 at 23:01
  • Added TemperHeader.
    – John Siu
    Mar 2, 2013 at 5:52
1

You could run a proxy server and make one browser use it and the other run direct. I'm not sure what off-the-shelf proxy would be best for this - if it's on a separate machine/VM then anything would be fine as you could configure the machines's DNS. Yoy can probably tell Apache or squid to use custom DNS or rewrite rules, or you could write your own proxy to do this in <100 lines of python...

1
  • +1, this is how we solved this problem in the field. You can then use a browser plugin to quickly switch proxies. Feb 25, 2013 at 10:44
1

One option is to set up your own DNS server and apply changes with dynamic DNS updates. You will likely still have to clear browser DNS caches due to DNS pinning. If you use a proxy, DNS is the proxy's responsibility (barring DNS prefetching issues in Chrome), so a proxy is the cleanest solution.

There are two HTTP proxies that can do this:

Fiddler is free, Charles is not, though it has a free time-limited trial.

In Chrome you can easily inspect and clear the DNS cache: chrome://net-internals/#dns I don't believe there's any way for an extension to override DNS (though it can make DNS lookups).

With a proxy-switching browser plugin you can switch sites by enabling or switching proxy.

For completeness, there are two possible problems with this approach: cookies and caching. State or auth cookies on one server might not work on the other. New content on your test site might not appear because it has not expired, this depends on the caching settings in the server and content. Flushing the browser cache and cookies should do the trick if this occurs.

You can also have the opposite problem of content not being cached as expected because the web servers have different Etag headers for the same static content. This doesn't usually cause a big problem, though it can get flagged in performance reports.

Since your current manual DNS switching approach will also potentially suffer from the above, they are probably not a problem for you, or you would have noticed already ;-)

(When possible I prefer to use an alternate approach to the whole problem which is usually cleaner: add virtual host aliases of "dev-www" "prod-www" "test-www" etc., though that sometimes has its own complications.)

1

How about just using a shell script or Applescript to swap the system hosts file?

Untested shell version:

mv /private/etc/hosts /private/etc/hosts.tmp
mv /private/etc/hosts.alt /private/etc/hosts
mv /private/etc/hosts.tmp /private/etc/hosts.alt
dscacheutil -flushcache
1
  • affects other browsers/programs. also does not necessarily refresh a DNS cache inside the browser
    – Thilo
    Mar 2, 2013 at 5:11
0

In FireFox, you could use the Redirector add-on that lets one specify patterns for pages that should be automatically redirected to other pages. For example redirect http://example.com/foo.html to to http://example.com/bar.html.

The extension supports both wildcards and regular expressions in patterns. Regular expressions support capturing parentheses, so you could redirect http://example.com/(.*) to http://1.1.1.1/$1.

To disable Redirector temporarily, click on the R in the status-bar or press Alt+R. Right clicking the icon opens the Redirector options window.

(Disclaimer: I have never used this add-on myself.)

1
  • That of course only works if typing in the IP address would have worked in the first place, i.e. not if the web site depends on the host name being present in the request.
    – Thilo
    Feb 26, 2013 at 0:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .