How can I redirect all URLs containing an IP address to my www domain?

apache, mod-rewrite, regex, url-rewriting

Solution

Note, you were using the wrong tag. It's not REMOTE_HOST as this is the user's ip. You need HTTP_HOST to get the server's IP.

For example::

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^\d+\.
RewriteRule (.*) http://www.example.com/$1 [R=301]

Edit, you can use a more specific regex if you want to. The main problem was your original tag.

Edit2:

If you have the list of specific IPs you want to redirect then you can use

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^111\.111\.111\.111 [OR]
RewriteCond %{HTTP_HOST} ^222\.222\.222\.222
RewriteRule (.*) http://www.example.me/$1 [R=301]

Problem

I have several servers with different IP addresses. I want to make it impossible to reach them via IP directly. Instead, I want them to redirect to my domain, www.site.com. The following condition matches an exact IP: ``` RewriteCond %{REMOTE_HOST} !^123\.456\.789\.012$ ``` How can I match any IP using Apache rewrite rules?

Original source