Redirect subdomain to another website

October 3, 2014 15.6k views

Hi - what is the easiest way to set up a subdomain like which I would want to redirect to ? I have tried a few things but cannot get it to work. Thank you!

2 Answers

I may have answered my own question. This approach seems to work, although, there may be a better alternative.

Set up the following DNS entry at     IN      CNAME       @

And then create / modify .htaccess as follows:

<IfModule mod_rewrite.c>
    RewriteEngine on
    Options FollowSymLinks
    RewriteCond %{HTTP_HOST} ^
    RewriteRule ^(.*)$ redirectToGoogle.html [L]

And then create redirectToGoogle.html and forward to the desired site

        <META http-equiv="refresh" content="0;URL=">

  • Cool. I looked around for a solution when I saw your question. most of what I tried didn't work. Nice work.

  • A better option would be creating a separate virtualhost for (see How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS | DigitalOcean) with the following config:

    <VirtualHost *:80>
        Redirect permanent /

    You will still need to create the DNS record, though.

    • Tried man but this isn't working. I created A record like pointing to IP and I setup the virtual host file and restarted apache. Can you please lay it down step by step so I can figure out where I missed? Please?

      • What happens when you browse to Do you get an error? If so, can you post it? Also, where did you create the virtual host file?

        • Hey appreciate the reply, I figured it out. The virtual file code you mentioned above works. I missed out to enable the virtual host file use a2ensite so it was my miss. But this works. Thanks a lot for the solution, greatly appreciate it :)

  • kamain7 i tried doing what you suggested with no success. could you assist me?

  • @galvax: Have you restarted Apache after adding the virtualhost?

    sudo service apache2 restart

    Did you create a DNS record for that points to your droplet?

  • That worked! but now, i want to mask the ip address so that instead of redirecting you to the ip address, i want to show the url as a subdomain. To do that i enter this into my DNS records.

    mysubdomain IN A CNAME mysubdomain

    Unfortunately this does not seem to work, Is there something wrong in my configuration? i am aware the ip address i put is a local one (i replaced it).

  • thank you kcristella, that's work.

  • thank you kcristella, that's work.

Have another answer? Share your knowledge.