Skip to content

Instantly share code, notes, and snippets.

@m5wdev
Created April 9, 2018 13:37
Show Gist options
  • Save m5wdev/015a516aa4c4ada4e413b6d705023c76 to your computer and use it in GitHub Desktop.
Save m5wdev/015a516aa4c4ada4e413b6d705023c76 to your computer and use it in GitHub Desktop.
Flask redirect from www to without www
# Redirect without WWW
from urllib.parse import urlparse, urlunparse
@app.before_request
def redirect_without_www():
urlparts = urlparse(request.url)
if urlparts.netloc == 'www.YOUR-DOMAIN.com':
urlparts_list = list(urlparts)
urlparts_list[1] = 'YOUR-DOMAIN.com'
return redirect(urlunparse(urlparts_list), code=301)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment