.htaccess - Rewrite IP to URL

I just recently found an issue where Google has indexed 4-5 pages of a domain by the servers IP address.

How do I enable mod_rewrite to be able to solve this issue from happening in the future?

Current .htaccess

RewriteEngine on
RewriteCond %{THE_REQUEST} /index.php
RewriteRule ^([a-z]+/)?index\\.php$ http://www.example.com/$1 [R=301,L]
RewriteRule ^(example-directory-1/)?index\\.php$ http://www.example.com/$1 [R=301,L]
RewriteRule ^(example-directory-2/)?index\\.php$ http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]

Easy fix… change this one line from:


RewriteCond %{HTTP_HOST} ^example.com [NC]

to:


RewriteCond %{HTTP_HOST} !^www\\.example\\.com [NC]

Out of curiosity, what does that do?

That specific line is to redirect non-www to www. How does that fix redirecting the IP address (if a crawler gets there for some reason) to the www version of example domain?

The way you had it, it was only redirecting no-subdomain (example.com) to [noparse]www.example.com[/noparse].

What I changed it to redirects anything except [noparse]www.example.com[/noparse] to [noparse]www.example.com[/noparse] (which includes straight ip-address hits).

Sorry for the delayed response!

Thank you for clarifying this issue. Now would this also redirect parked domains that are creating duplicate content? For example, I have example.com as my main domain as well as example.net and example.org.

Reason I ask, is because I parked those domains, they redirected the homepage, but subdirectories would go to the non .com domain without redirecting or being rewritten.

Yeah. It’ll redirect everything as it should to avoid content dilution in googles eyes. Add a [L,R=301] to the end of the rule to issue a 301 permanent redirect.

Sorry, you had that correct in your original… was posting from my phone again lol.

Ok, so now this is my exact code for redirection…

RewriteCond %{HTTP_HOST} !^www\\.equotemd\\.com [NC]
RewriteRule .? http://www.equotemd.com%{REQUEST_URI} [R=301,L]

I tested it last night, it works for the IP address redirection. My next test will be checking out the parked domains. I will have to change the name servers to point to my server, park them and then await propagation.

Help was appreciated!

I just accidentally posted a response in the wrong thread regarding this issue…

With these new changes to my .htaccess file, I cannot keep a subdomain from redirecting back to my root domain. How do I fix this issue?

Just add a RewriteCond


RewriteCond %{HTTP_HOST} !^somesubdomain\\.equotemd\\.com [NC]
RewriteCond %{HTTP_HOST} !^www\\.equotemd\\.com [NC]
RewriteRule .? http://www.equotemd.com%{REQUEST_URI} [R=301,L]

You need one RewriteCond for every subdomain you want to exclude, or put them together in one rule like so


RewriteCond %{HTTP_HOST} !^(www|somesubdomain)\\.equotemd\\.com [NC]
RewriteRule .? http://www.equotemd.com%{REQUEST_URI} [R=301,L]

Scallio… You are now officially my .htaccess hero of the day!

Sad part is, I think I actually knew how to make that edit… Lol.

:slight_smile: