Rewrite IP address of a host to another IP address with .htaccess

9,554

If you can use RewriteRule in your .htaccess file, you can't "redirect" per se, but you can reverse-proxy the connections to 123.123.123.123 out to 234.234.234.234, so that you only have to maintain the single website.

RewriteEngine On
RewriteRule ^/(.*)$ http://234.234.234.234/$1 [P]

This assumes that host 123.123.123.123 can connect to host 234.234.234.234 directly. Connections from users who are getting "123.123.123.123" as the IP for your DNS name will go to the old server, but then Apache will make a request on their behalf to the new server and return the results of that request back to the user.

An important side effect of this is that the client IP will get lost in the shuffle, as host 234.234.234.234 will see the requests as all coming from host 123.123.123.123. However, Apache does set an X-Forwarded-For header which you can log if you choose.

WARNING: The one possible snag in this setup is that the connections coming into 234.234.234.234 will send a Host header of "234.234.234.234" which may be okay if your site is the only one on that IP, but if your new hosting location is using named virtual hosts, it will likely fail.

In that case, the best option would be to use a different DNS host. You should be able to set the authoritative name servers on your DNS record to point to somewhere else, either your new web hosting provider might provide this service, or for maximum control you can take over control of your records yourself using a service such as Amazon Route 53.

Share:
9,554

Related videos on Youtube

Yusuf
Author by

Yusuf

Updated on September 18, 2022

Comments

  • Yusuf
    Yusuf over 1 year

    I have to redirect content on a webserver with a hostname (mydomain.com) from current IP (123.123.123.123) to a new IP (234.234.234.234), while keeping the same hostname, using .htaccess. How can I do that?

    • Sumeet Kashyap
      Sumeet Kashyap about 12 years
      I think this is impossible under the constraints described: if the DNS has mydomain.com -> 123.123.123.123, then for the client to access a different server you have to either change the DNS or change the hostname. You could have the server proxy one to the other, but I'm not sure if that's possible within htaccess.
    • Yusuf
      Yusuf about 12 years
      OK, then let's say I have created a sub domain for the new server; how do I redirect the mydomain.com to sub.mydomain.com while still showing the user mydomain.com for all the links?
    • ravi yarlagadda
      ravi yarlagadda about 12 years
      You could proxy the user.. but can you clarify why you wouldn't simply change the DNS?
    • Yusuf
      Yusuf about 12 years
      Well, the thing is, I've already changed the DNS, but our ISP is Mauritius has 3 DNS servers; two of them are pointing to the new IP address, while only one of them is still pointing to the old one. We have also contacted them to try solving the issue, but they're so slow that we're trying other solutions in the meantime. You have anything else to propose? What do you mean by proxying the user?
  • Yusuf
    Yusuf about 12 years
    Yes, I already know that and I have done it several times; but with this particular domain, the change is not being propagated to one of our local (Mauritius) ISP's DNS servers.
  • Yusuf
    Yusuf about 12 years
    This looks like it could help, although I'm using Virtual Hosts like you said; I'll still try it though, and see what gives.
  • uset631458
    uset631458 about 12 years
    This may totally mix your stats if you use awstats too, because your rewritelogs will see all incoming connexions from 123.123.123.123 (= the real referer will be lost). Tell me if I'm wrong.
  • Jenny D
    Jenny D about 10 years
    Note that this requires you to have mod_proxy as well as mod_rewrite available.