1

I am attempting to redirect the IP address of my domain to the domain name and am running into trouble. The IP address does not redirect to the domain name listed in the redirect statement below.

The IP Address is 123.123.123.123

My .htaccess looks like below

RewriteCond %{HTTP_HOST} ^123\.123\.123\.123$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
1
  • When you access your site by IP address, do you actually get to your website? That is not the case for me. (In fact I get a virus/malware warning!) – MrWhite Oct 18 '14 at 12:08
2

Ordinarily your code should work OK. Although the NC (NOCASE) flag on a numeric value is unnecessary and should be removed.

Also note that if you are checking for an exact match (such as a complete IP address) it is usually easier to simply use the = (equals) operator, for example:

RewriteCond %{HTTP_HOST} =123.123.123.123

This now matches against the plain string "123.123.123.123" (exact match), not a regular expression, so there is no need to escape the dots.

Your site also appears to be example.com, not www.example.com so you should change the substitution in the RewriteRule to reflect this, otherwise this will result in a second redirection.

However, the main problem in your case seems to be that you are on a shared host - multiple websites hosted on the same IP address (in fact, a rough estimate shows that there are over 500 websites on that IP address). Requesting your server's IP address does not return your website, so attempting to do this kind of redirection is not going to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.