I want that when people access my web server with the IP address, like http://10.0.0.1
, be redirected to the domain name, like http://example.com
. I'm using Apache Web Server and I've tried:
<VirtualHost 10.0.0.1:80>
Redirect / http://example.com
</VirtualHost>
which generated an infinite redirect loop because it has higher priority than
<VirtualHost *:80>
ServerName example.com
...
</VirtualHost>
which is the virtual host I ultimately want to serve.
I've also tried
<VirtualHost 10.0.0.1:80>
ServerName 10.0.0.1
Redirect / http://example.com
</VirtualHost>
thinking that maybe it'll only match http://10.0.0.1
but not http://example.com
. But it also generates an infinite loop. Any ideas how to achieve that?
The reason why the IP virtual host is not the main and only virtual host is because I want other IPs on the server to also be able to serve example.com
.
HD answered gave a good rewrite rule, but you should put a condition before that. Make sure that there is only one virtual host running on 10.10.10.1 and have your definition look like the following:
This way, you only need one virtual host for that IP address instead of what you were trying to do above. NOTE: If you're going to have more than one virtual host on this server, you need to put the rest on different IP(s)
I try the same configuration and it works fine. Are you sure there is no other definition that conflicts with your virtual hosts? What is the error in the apache logs?
This rule also works:
In
.htaccess
add the following: