Chris Hawkes
Chris Hawkes

Reputation: 12410

How to redirect from appspot domain to custom domain?

I found this post from Amir in regards to redirecting request from google.appspot domain to the custom domain. My question is where do you put something like this using Web2py?

**To just add a custom domain, just follow the instructions here: http://code.google.com/appengine/articles/domains.html
And once that works, you can put a check in your code to forward anyone landing on the appspot.com domain to your domain: (example in python)
def get(self):
  if self.request.host.endswith('appspot.com'):
    return self.redirect('www.jaavuu.com', True)
  # ... your code ...**

Upvotes: 1

Views: 1705

Answers (3)

bourne2program
bourne2program

Reputation: 131

With webapp2 here is something like what I did, where BaseHandler is the type of all my handlers:

class BaseHandler(webapp2.RequestHandler):
    def __init__(self, request, response):
        self.initialize(request, response)
        if request.host.endswith('appspot.com'):
            query_string = self.request.query_string
            redirect_to = 'https://www.example.com' + self.request.path + ("?" + query_string if query_string else "")
            self.redirect(redirect_to, permanent=True, abort=True)

Upvotes: 0

Anthony
Anthony

Reputation: 25536

At the beginning of your first model file, you can do:

if request.env.http_host.endswith('appspot.com'):
    redirect(URL(host='www.yourdomain.com', args=request.args, vars=request.vars))

This will preserve the entire original URL, except for replacing yourdomain.appspot.com with www.yourdomain.com. Note, URL() will automatically fill in the current controller and function, but you have to explicitly pass the current request.args and request.vars to make sure they get preserved.

Upvotes: 3

Sologoub
Sologoub

Reputation: 5352

That goes into your request handler.

Using example from web2py documentation:

Example 8

In controller: simple_examples.py

def redirectme():
    redirect(URL('hello3'))

You'd want to do something like this:

def some_function():
    if request.env.http_host.endswith('appspot.com'):
        redirect(URL('www.yourdomain.com'))

Upvotes: 1

Related Questions