Redirect subdomain to webapp

January 31, 2014 1.7k views
I would like to redirect a subdomain to an URL of a webapp. Something like: >>>> How can I do it?
2 Answers
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.


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 containing the following:
header('Location: ');

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 :
location / {
    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 in the URL for your web users.

Hope this could help,



Have another answer? Share your knowledge.