Apache VirtualHost – redirect from one domain to a specific page of another one *transparently*

apache-2.2redirect

I would like to redirect www.example.it to www.example.com/index-it.php

The website is just a one-page site.

I would like that to happen transparently, in the sense that www.example.it will be still displayed on the address bar after the redirect.

I have tried this but it generates an Internal Server Error.

<VirtualHost *:80>
ServerName www.example.it

RewriteEngine on
RewriteRule ^/.*$ http://www..com/index-it.php [P,L]

</VirtualHost>

This would work instead, but it doesn't provide a transparent redirect:

<VirtualHost *:80>
ServerName www.example.it

RewriteEngine on
#RewriteRule ^/.*$ http://www.example.com/index-it.php [P,L]

Redirect 301 / http://www.example.com/index-it.php

</VirtualHost>

Any idea, please? Thanks.

Best Answer

I suggest that you don't actually want to redirect, because "redirect" actually means "tell the browser to request this other page instead."

Also, I'm betting you don't actually want to redirect all requests, because if your browser URL bar shows www.example.it and the page it has loaded has a reference to /logo.png, the browser will request http://www.example.it/logo.png and it will be surprised to get the output of www.example.com/index-it.php in response.

Because you're using the [P] flag, the approach you're actually taking is reverse proxing all requests to www.example.it to www.example.com, and before proxying, internally rewrite some subset of index requests on www.example.it to /index-it.php. The When Not To Use Rewrite page lists "proxying" as one of its times not to use mod_rewrite. mod_proxy by itself wil help you with the proxying; mod_rewrite's documentation actually suggests preferring mod_proxy directives over the P flag.

Read mod_proxy's warning about being sure your server is secure before you turn it on. The index-file juggling may be a reasonable use of mod_rewrite, but it doesn't require use of the P flag.

Related Topic