Last Updated on August 20, 2021 by Amit

This article explains how to redirect an IP hostname to domain name using a RewriteRule directive in htaccess file.
Every domain name on the internet has an unique IP address assigned to it that connects the domain to internet.
If your website is accessible with both an IP address like (192.66.77) and domain name (example.com) then, in order to maintain the SEO , its important that you either redirect your domain to ip address or the IP address to domain.

URL redirection from domain name to IP address is uncommon as nobody will want the IP address to appear on their browser address bar as it might be hard for your visitors to remember it. Domain name on the other hand is easier to remember and type.

IP address to domain name redirection with htaccess

With an .htaccess file you can easily redirect your domain ip address to domain name.
You can simply use a RewriteRule for this purpose.

The rule bellow 301 redirects domain IP address to domain name.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^192\.1.55.88$
RewriteRule (.*) https://domainname.com/$1 [L,R=301,NE]

Explainations :

The rule above will match any http_host string that looks something like . In this example , it matches the http_host ip address and if the match if found then the RewriteRule line is read. The rule redirects the ip host domain to domainname .
In simple words the code above will redirect any request from to domainname.com/foobar .
The R=301 in the rule above represents a permanent redirection with 301 status code . The R=301 is important here as it tells search engines to update the results with a new domain.

A solution for Apache 2.4 users

If your Apache version is 2.4 or above you can also use Redirect with IF directive that’s supported on Apache 2.4 or higher.

The following should work on Apache 2.4* :

RewriteEngine On

 <If "%{HTTP_HOST} == ''">
 RedirectMatch 301 ^/(.*)$ https://example.com/$1

Related :

.htaccess tutorial for mod_rewrite beginners .

How useful was this post?

Click on a star to rate it!

Average rating 4.3 / 5. Vote count: 7

No votes so far! Be the first to rate this post.