paranoic
paranoic

Reputation: 33

.htaccess redirect from IP adress to actual domain

How do I redirect IP to actual domain? For example:

123.45.678.901 to www.example.com

and all subfolders and subpages like

123.45.678.901/all-pages to www.example.com/all-pages

Also, my SearchConsole also recognizes "top linking sites" for the same IP but in this manner:

IP 123.45.678 ---------------------------- 38,132 LINKS

IP 123.45.678.901 ------------------------- 3,617 LINKS

So I probably should redirect 123.45.678 as well. Is that possible and how?

One more thing - IP opens in HTTP and domain opens in HTTPS protocol.

Upvotes: 1

Views: 2641

Answers (2)

paranoic
paranoic

Reputation: 33

Here is the solution to this kind of problem

In the .htaccess file put next code:

RewriteBase /
RewriteCond %{HTTP_HOST} ^123\.45\.678\.901$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

So if you have a URL wit IP like

123.45.678.901/all/other/pages/

after implementing this code all pages are going to be redirected to

www.example.com/all/other/pages/

I hope i could help anyone because this is one of very important factors which may ruin your SEO.

Upvotes: 0

arkascha
arkascha

Reputation: 42875

Your comment to your question indicates that this is much easier than the description in the question itself indicates... The following should point you into the right direction, though you may have to tweak it for your situation. It odes not implement any specific address (which might change) but instead redirects every requested host name (so also ip addresses) that do not match your desired host name. In case you want to add exceptions you can di that using RewriteCond directives, for example in case your http server serves multiple host names / domains.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [QSA,R=301]

It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...

This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Upvotes: 1

Related Questions