Redirect IP address to Domain name Apache

December 18, 2014 27.6k views


Since I might host several static websites on the same droplet I've used apache's virtual host. As a result, in my /var/www I have folders that correspond the domain name. For example: /var/www/example.com/public and /var/www/another.com/public.

in my sites-available (/etc/apache2/sites-available) I have:

• 000-default.conf
• default-ssl.conf
• example.com.conf
• another.com.conf

example.com.conf and another.com.conf are configured just fine. They redirect to the desired folder in /var/www and everything works perfectly fine. However, when I try to access my IP address directly, it points me to /var/www/html because that's how it's set in 000-default.conf.

I've edited 000-default.conf and changed its DocumentRoot to be /var/www/example.com/public as I wish this website to be the "main" website when requesting the domain.

As expected, it works just fine. However, I've noticed that it keeps the IP address in the URL. I tried several things to make it change the URL to example.com just as if I was accessing example.com from the URL directly and not the IP address.

Unfortunately, I couldn't get it work. Any idea how I can implement it without using some "hacks" such as directing the IP address to /var/www/html and store in there a .htaccess that will redirect me to example.com. I would like to store that logic in the 000-default.conf file.

Thanks in advance!

4 Answers

You can achieve this with a rewrite in an htaccess file in your /var/www/example.com/public directory. First you will need to be sure that your 000-default.conf file has AllowOverride All set foorr this directory so the settings in your htaccess file will be used. Then you will want to create an .htaccess file in that directory with the following (replacing the IP and domain with your own):

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^12\.34\.56\.789$
RewriteRule ^(.*)$ http://www.domainname.com/$1 [L,R=301]

once this is done, make sure the rewrite module is enabled:

a2enmod rewrite

and restart apache

service apache2 restart
  • @ryanpq Thanks for the reply!

  • Ryan,

    How/where do you place this in a .htaccess file for a WordPress installation?

  • @Stooky For a wordpress installation you would need to add this to the .htaccess file in the web root. (default with apache is /var/www/html). If you change permalinks settings Wordpress may overwrite this file so make sure you have set up your permalinks the way you want them before modifying the file.

  • worked like a charm. thank you.

  • I have the same problem as OP and I tried this, but if I connect to my server by entering the IP address in the address bar of my browser, when it connects I still see the IP address in the address bar, and not the domain name. What should I do? For the record I only have a single domain name and a single website, which I'm running out of /var/www/html/, built on WordPress.

That's what I did, but it "breaks" the site.

WordPress by default has some Rewrite conditions. Here is the default WordPress Rewrite section.

BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUESTFILENAME} !-f
RewriteCond %{REQUEST
RewriteRule . /index.php [L]

END WordPress


  • Have you found a solution for the broken site outcome Stooky? I am experiencing the same issue when adding a rewrite condition in .htaccess


I cannot remember for certain what I did (I did resolve it), but make sure you have mod_rewrite enabled for apache.


Have another answer? Share your knowledge.