13

Google search results are showing my pages as (ip)/mypage.html instead of https://www.mydomain.com/mypage.html. I believe the solution is to redirect the ip's to the domain. I've found many, very similar ways to do this, but none of them are working for me. I have an existing rule that redirects http to https. This is what my .htaccess file currently looks like:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.111$ [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^([a-z.]+)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]

What am I doing wrong?

1
  • 1
    Adding a redirect won't change the links that Google believes works. Obviously Google has indexed some other page on the internet that links to your IP address. Apr 3, 2012 at 0:49

4 Answers 4

25
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^111\.111\.111\.111
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]

Alter "111" to your IP

2
  • 3
    this worked better! Thanks. how about something like 111.111.111.111/* Can you update it or give a suggestion ? Oct 12, 2015 at 11:52
  • thanks a lot! The gerban's answer didn't work for me... just because of RewriteCond %{HTTP_HOST} ^([a-z.]+)?mydomain\.com$ [NC] line
    – Ali
    May 2, 2019 at 7:54
16

Your 2 rewrite conditions clash. They require http_host to be 11.11.11.111 and to be *.mydomain.com, at the same time. Just add an or like so:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.111$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
6
  • Thank you! Now will Google adjust, or is there something else I need to do? All of my pages show up as (ip)/mypage.html.
    – user617123
    Apr 4, 2012 at 19:07
  • A 301 should do that with google. Though I've never seen this with naked IP's, but I think it will work just fine. Make sure to check every now and than for the next few weeks.
    – Gerben
    Apr 5, 2012 at 16:48
  • What's the definition of a 301? Does the above solution count? My domain still won't show up in search results, only IP's.
    – user617123
    Apr 11, 2012 at 21:47
  • 1
    Give it some time. Otherwise create an account at google.com/webmasters
    – Gerben
    Apr 12, 2012 at 16:12
  • 2
    @Gerben, How to redirect 192.168.0.5/anyurl to https://example.com/anyurl ?
    – Victory
    Dec 27, 2019 at 6:58
1

update to willian's answer. you just need to replace the domain name (your-domain.io) from this snippet.

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
RewriteRule (.*) https://your-domain.io/$1 [R=301,L]
1
  • 1
    Just a warning that this will force even internal hits to localhost 127.0.0.1 to also redirect, which could screw up monitoring services, which rely on being able to hit, for example, 127.0.0.1/server-status?auto to see site status, resulting in errors. It's probably a better idea not to use regex and hardcode the IP address specifically to redirect.
    – cdabel
    Apr 29, 2021 at 3:55
0

Hi there we had requirements to do this as well due to a trigger index in the main pub_html folder. These rules should mask the IP to the http (or https if you switch em), make non-www into www. This should also preserve subdomains. This is for (1) simple site sitting in the pub_html, so test with care if you have subdomain triggers or whatever else in your htaccess:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ https://www.exampledomain.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^50\.28\.55\.76$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?exampledomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://www.exampledomain.com/$1 [R=301,L]

Hope it helps and works for ya'll. Thanks for the thoughts.

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.