Tweetegy On the edge of chaos with Blockchain, Dapps, DeFi, JavaScript

| About | Search | Archive | Github | RSS |

How to subdomain redirect using Apache Virtual Host

Do you want to setup a website subdomain so that, for example, will actually, without the end user realizing, return content from ? In this short, but useful blog post, I will show you how you can redirect a subdomain to a server directory without anyone (except you) knowing!

Assuming that you already have your subdomain DNS settings correctly configured and the subdomain now points to the IP address of your server, you should do the following in Apache running on an Ubuntu server.

Open the configuration file for your site. In this example, we will use the default site.

nano /etc/apache2/sites-available/default

Add the following additional VirtualHost configuration to the end of the file.

<VirtualHost *:80>
  DocumentRoot /var/www/foo

Now restart apache and your all done!

/etc/init.d/apache2 restart

If you point your browser to you will see the content from ! Note: You do, of course, have to create the folder /var/www/foo and put a index.html file there with some content like “Here be foo!” so you can test it actually works!

Extra Bonus Script Time! As an added bonus, I have included a setup script that you can use to perform the above in one shot. Use it if you wish. Change foo and to your domain and subdomain names.



echo "Configure default site with foo virtual host"
if ! grep -q "ServerName" $SITE_CONFIG_PATH
	echo "Configuring..."
	echo "" >> $SITE_CONFIG_PATH
	echo "# foo virtual host" >> $SITE_CONFIG_PATH
	echo "<VirtualHost *:80>" >> $SITE_CONFIG_PATH
	echo "  ServerName" >> $SITE_CONFIG_PATH
	echo "  DocumentRoot /var/www/foo" >> $SITE_CONFIG_PATH
	echo "</VirtualHost>" >> $SITE_CONFIG_PATH
	echo "Restart apache"
	/etc/init.d/apache2 restart
	echo "Already configured!"