就配置而言,有人指出的 WebFaction 讨论是正确的,您只需要自己应用它,而不是通过控制面板。
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
放入 .htaccess 文件,或适当上下文中的主要 Apache 配置。如果在主要 Apache 配置中的 VirtualHost 内部,您将 ServerName 设置为 www.example.com 并将 ServerAlias 设置为 example.com 以确保虚拟主机处理这两个请求。
如果您无权访问任何 Apache 配置,如果需要,可以使用围绕 Django WSGI 应用程序入口点的 WSGI 包装器来完成。就像是:
import django.core.handlers.wsgi
_application = django.core.handlers.wsgi.WSGIHandler()
def application(environ, start_response):
if environ['HTTP_HOST'] != 'www.example.com':
start_response('301 Redirect', [('Location', 'http://www.example.com/'),])
return []
return _application(environ, start_response)
修复此问题以在站点中包含 URL 并处理 https 留给读者作为练习。:-)