Make ip address redirect to real name with Apache
Solution 1
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:
<VirtualHost 10.0.0.1:80>
ServerName servername.com
.....
RewriteCond %{SERVER_NAME} 10.10.10.1
RewriteRule /(.*) http://servername.com/$1 [R=301,L]
</VirtualHost>
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)
Solution 2
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:
RewriteRule ^(.*)$ http://example.com/$1 [R=301,NC]
Related videos on Youtube
Pablo
You can find my blog at https://pupeno.com where I publish about coding and other stuff.
Updated on September 17, 2022Comments
-
Pablo over 1 year
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, likehttp://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 nothttp://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
.-
radius almost 15 yearsWhat about your NameVirtualHost setting ?
-
MrWhite almost 6 yearsYou should be able to do this using name-based Virtual Hosts (default on Apache 2.4+). See this more recent ServerFault question: serverfault.com/questions/914649/…
-
-
Pablo almost 14 yearsShouldn't the rewrite condition try to match the virtual host ip, like 10.0.0.1 instead of 10.10.10.1? Why would other virtual hosts need to run in different IPs? Won't they match by name instead of IP?
-
lanoxx about 12 yearsIn my case it was also neccessary to add
RewriteEngine On
before the RewriteCond line. -
FooBee about 8 yearsWhat does this add to the answer from seven years ago?
-
MrWhite almost 6 years(Where did
10.10.10.1
come from?) In a VirtualHost context, that is accepting requests to a specific IP address, there is no need to check the IP address in the mod_rewrite directive. (But you should probably be checkingHTTP_HOST
rather thanSERVER_NAME
- which is dependent on the server config.) But checking for the IP address in theHost:
header won't catch HTTP/1.0 requests (which don't include theHost:
header). -
MrWhite almost 6 yearsThis is specific to
.htaccess
(to which it does add "something"). However, the question is really concerned with handling this in the main server config / virtual host. -
MrWhite almost 6 yearsMinor point, but if this is in a virtualhost context, as suggested by the question, then you should either include a slash prefix on the
RewriteRule
pattern (ie.^/(.*)
) or omit the slash at the start of the URL-path in the substitution. Otherwise, you'll get a double-slash in redirected URL. -
MaXi32 over 4 yearswhat about ipv6? do you have answer for this?
-
MaXi32 over 4 yearswhat about ipv6 condition?