How to redirect an IP address to a domain using mod_rewrite?

Sometimes you need to redirect an ip address to a domain for SEO purposes or for many other reasons, for example an account with a dedicated ip to the domain name. This can be performed easily using mod_rewrite.

For example if your ip is 10.10.10.10, accessing to http://10.10.10.10 will redirect you to http://yourdomain.com

Simply add the following lines in the .htaccess file in your public_html directory changing the domain and the ip number with your information:

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

Using this rule if http://10.10.10.10 is accessed then a 301 redirect is created to point to http://yourdomain.com

Was this answer helpful? 4 Users Found This Useful (1066 Votes)