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.
Webmasters Stack Exchange is a question and answer site for webmasters. It only takes a minute to sign up.
Sign up to join this communityI 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.
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)