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:
/etc/apache2/sites-available) I have:
• 000-default.conf • default-ssl.conf • example.com.conf • another.com.conf
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 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
Thanks in advance!
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.×