Report this

What is the reason for this report?

Redirect subdomain to webapp

Posted on January 31, 2014

I would like to redirect a subdomain to an URL of a webapp. Something like:

newsub.domain.com >>>> another.domain.com:8080/myWebApp

How can I do it?



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!

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.

Do you want this to be permanent or temporary? There are various ways to do this. You could create an index file in the document root that redirects, setup rewriting of a location in the virtual host depending on what web server you use, etc.

Hi,

To do so, you have 2 solutions :

  • If you want your users to see the URL as another.domain:8080/myWebApp explicity, you can use a simple PHP redirect placing a index.php file in the docroot ofr newsub.domain.com containing the following:
<?php
header('Location: http://another.domain.com:8080/myWebApp ');
exit;
?>

This solution is not the smartiest one, as it shows an unusual port in the URL for your webapp.

  • The second solution could be using Apache or Nginx as a reverse proxy for your webapp. With nginx configuration file, this would be a configuration of that kind, to set on the server hosting newsub.domain.com :
location / {
    proxy_pass http://another.domain.com::8080/myWebApp;
    proxy_cache cache;
    proxy_cache_valid 12h;
    expires 12h;
    proxy_cache_use_stale error timeout invalid_header updating;
}

I would rather opt for the 2nd solution, as the reverse proxy will conserve newsub.domain.com in the URL for your web users.

Hope this could help,

Regards,

– rustx

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.