1

I am moving a website from one shared host to another.

The site is all files. I have already copied the files to the new host.

I'd like to redirect all HTTP requests from the old host to the new host, so I can remove the site on the old host and maintain just the copy on the new host, even before the DNS records have been updated to point to the new host.

The old host is running Apache 2.2, so this can be done by adding the following to the VirtualHost section(s) for my site:

ProxyPreserveHost On
ProxyPass         / http://ipaddress.of.my.new.host/

The ProxyPreserveHost directive is required here. It allows the new host to find the files for my site.

Now what if I can't edit these configuration files?

Can the same effect be achieved from an .htaccess file?

Instead of the ProxyPass directive, I have

RewriteRule ^(.*) http://ipaddress.of.my.new.host/$1 [P,L]

That works fine. But how to preserve the Host: header? Adding

Header set Host my.domain.name

has no effect.

(See also this question and this question.)

UPDATE: as w3dk noted below, the directive to set a request header is RequestHeader, not Header, but using

Request set Host my.domain.name

has no effect, either.

1 Answer 1

1

As far as I know the proxy target in mod_rewrite is simply a front-end to mod_proxy and the ProxyPreserveHost is the only directive to configure that behaviour. So without access to the main server config or VirtualHost config your indeed without luck.

You might be lucky and your old hosting provider may already resolve your site to the new ip-address; you could then use the following work-around:

RewriteRule ^(.*) http://<your_domain_name>/$1 [P,L]
4
  • I really want to know what to do in the specific situation where the DNS records still point to the old host, while the content is already on the new site. I don't udnerstand why adding a Host: header in .htaccess doesn't work. Feb 1, 2016 at 16:20
  • @reinierpost By default the Header directive executes late in the request in order to send a response header back to the client. mod_proxy has already executed by this stage. But anyway, mod_headers is not intended to influence the request headers sent in the proxy request, for that you would need to use the RequestHeader directive. But I doubt you would be able to influence the Host: header using this directive.
    – MrWhite
    Feb 1, 2016 at 17:33
  • Whoops - of course. Thanks. I suppose this means the only option is writing a proxy myself? (E.g. a PHP script.) Feb 1, 2016 at 18:27
  • 1
    In serverfault.com/questions/557233/… they say you can use that only when ProxyPreserveHost On is set, and my test confirms this ... Feb 1, 2016 at 18:36

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.