Using mod_rewrite to redirect an IP address to a domain name

To redirect an IP address to a domain name on an apache webserver using .htaccess and mod_rewrite the following can be done.

Lets say you have the domain name domain.com on IP address 4.2.2.2 and you do want to redirect the IP address to the domain name if just the IP is accessed (say for SEO purposes). That would be http://4.2.2.2 will rewrite to http://domain.com. In your .htaccess file add

RewriteEngine On
RewriteCond %{HTTP_HOST} ^4\.2\.2\.2
RewriteRule (.*) http://domain.com/$1 [R=301,L]

Using the above rule if http://4.2.2.2/filename is accessed a 301 redirect is created to point to http://domain.com/filename


Interserver
http://www.interserver.net

Have we provided you with the highest level of service? Like us on Facebook or write on our wall. www.facebook.com/interserver

Visit our new community forums at http://forum.interserver.net/forum/