2

I see loads of tutorials about naked domain to www domain redirects, but what if I want to do it the other way around (www.example.com -> example.com)?

My app is on Google Apps with Flask deployed, and my domain is registered with GoDaddy.

  • 1
    I'm not using vhost or apache – Jimmy May 27 '15 at 15:52
  • What web server are you using? This will help. I am sure we have an IIS and Nginx example somewhere. – closetnoc May 27 '15 at 15:53
  • 1
    I'm using a google cloud app in flask – Jimmy May 27 '15 at 15:55
2

www. is an automatic alias. (See http://www.chickenaday.appspot.com/ vs http://chickenaday.appspot.com/). Why force the user?

If you do want to redirect, you could try this untested snippet of slightly modified code from https://stackoverflow.com/a/10964868/3164117:

from urlparse import urlparse, urlunparse

@app.before_request
def redirect_nonwww():
    """Redirect www requests to non-www."""
    urlparts = urlparse(request.url)
    if urlparts.netloc == 'www.example.com':
        urlparts_list = list(urlparts)
        urlparts_list[1] = 'example.com'
        redirect(urlunparse(urlparts_list), code=301)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.