views:

45

answers:

3

Sometimes its the simplest things that trip me up.

I have an application running on App Engine that utilizes subdomains. I'd like to redirect to different HTML pages depending on weather a subdomain is being used or not.

For example, if a user tries to sign up for a new account through a subdomain, this doesn't make a whole lot of sense, so I'd like to redirect to the signup page on the promary domain:

user_subdomain.main_domain.com/signup_for_new_account.html
----redirect to-->
main_domain.com/signup_for_new_account.html
A: 

You can do this client-side with javascript:

if (location.hostname !== 'main_domain.com') {
  location.href = 'http://main_domain.com' + location.pathname;
}

Doing the same thing server-side is trickier.

Routing is handled based on the path only, not the hostname. Within app.yaml, it's not possible to assign different handlers to the same path on different hostnames. You would need to point /signup_for_new_account.html to a dynamic handler that first checks the hostname and then either issues a redirect or proxies to the static content.

Drew Sears
Pushing design issues on the server side through to your user's browser is a _terrible_ idea - from the school of design that brought us asp.net sending 302 redirects to error URLs.
Nick Johnson
+1  A: 

In Python you can check os.environ['SERVER_NAME'] == 'main_domain.com', then redirect.

For Java I think the request has a getServerName method that returns the server name. Then you can redirect, which basically means setting a few headers and returning.

Robert Kluin
+1  A: 

I discussed subdomain routing here. In a nutshell, you can write your own (fairly simple) WSGI middleware that takes care of routing different subdomains to different WSGI applications.

Nick Johnson