Redirect subdomain to another website

October 3, 2014 11.1k views

Hi - what is the easiest way to set up a subdomain like apps.domain.com which I would want to redirect to https://www.google.com/a/domain.com ? 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 domain.com

apps.hfadm.org.     IN      CNAME       @

And then create / modify .htaccess as follows:

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

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

<html>
    <head>
        <META http-equiv="refresh" content="0;URL=http://yoursitehere.com">
        </head>
</html>

  • 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 apps.domain.com (see How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS | DigitalOcean) with the following config:

    <VirtualHost *:80>
        ServerName apps.domain.com
        Redirect permanent / https://www.google.com/a/domain.com
    </VirtualHost>
    

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

    • Tried man but this isn't working. I created A record like subdomain.main.com 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 subdomain.main.com? 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 apps.domain.com 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 192.168.1.100
    cloud.mydomain.com 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.