Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
I think this is impossible under the constraints described: if the DNS has ->, 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. – pjc50 Feb 27 '12 at 13:47
OK, then let's say I have created a sub domain for the new server; how do I redirect the to while still showing the user for all the links? – Yusuf Feb 27 '12 at 17:20
You could proxy the user.. but can you clarify why you wouldn't simply change the DNS? – Shane Madden Feb 27 '12 at 17:57
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 Feb 28 '12 at 4:14

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

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

This assumes that host can connect to host directly. Connections from users who are getting "" 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 will see the requests as all coming from host 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 will send a Host header of "" 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|improve this answer
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. – Yusuf Feb 28 '12 at 16:09
This may totally mix your stats if you use awstats too, because your rewritelogs will see all incoming connexions from (= the real referer will be lost). Tell me if I'm wrong. – Olivier Pons Feb 28 '12 at 22:38
Note that this requires you to have mod_proxy as well as mod_rewrite available. – Jenny D Apr 5 '14 at 8:29

The problem is not about your rewriterule, it's about your DNS configuration.

My own DNS configuration is something like:

$TTL 86400
@   IN SOA (2011111300 86400 3600 3600000 86400)
            IN  NS 
            IN  NS 
            IN  A 
*           IN  CNAME 

This means "if you type or whatever behind i.e. or go to the server at the adress

If I change the adress to a new one for example, this will work and as soon as you'll type or any other stuff (like explained before) it will go to Everything will work smoothlessly without touching any .htaccess file.

share|improve this answer
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 Feb 28 '12 at 16:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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