A simpler solution than creating a standalone Heroku application would be the before_request function.
from urlparse import urlparse, urlunparse @app.before_request def redirect_nonwww(): """Redirect non-www requests to www.""" urlparts = urlparse(request.url) if urlparts.netloc == 'example.com': urlparts_list = list(urlparts) urlparts_list[1] = 'www.example.com' return redirect(urlunparse(urlparts_list), code=301)
This redirects all non-www requests to www using the HTTP 301 Moved Permanentently response.
Danilo bargen
source share