Question

Redirect IP address to Domain name Apache

Hello!

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!


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/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

Dear all After I try at apache 2.2 in centos 6.6, I got the error code as shown as the link of https://drive.google.com/open?id=1ECqgv2OMOasblGl66ZZTBWQ_7mvUCbK-

My code is as listed as the link https://drive.google.com/file/d/1VCj8xO8HUr7b98ys0KCSCLz2u3MOEr4w/view?usp=sharing

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

Kwaifeh,

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

http://c3p.me/1HOi6ap