Redirect IP address to Domain name Apache


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/ and /var/www/

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

• 000-default.conf
• default-ssl.conf
• and 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/ 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 just as if I was accessing 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 I would like to store that logic in the 000-default.conf file.

Thanks in advance!

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

You can achieve this with a rewrite in an htaccess file in your /var/www/ 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 ^(.*)$$1 [L,R=301]

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

a2enmod rewrite

and restart apache

service apache2 restart

Dear all After I try at apache 2.2 in centos 6.6, I got the error code as shown as the link of

My code is as listed as the link

Is there suggestion for the problem? Thanks for your kindly assistance.


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