[英]Django Method of Forwarding Non-www to www?
In django I am wondering if there is a django-designed way to redirect http://www.example.com to http://example.com or the other way around http://example.com > http://www.example.com . 在django中,我想知道是否有django设计的方法将http://www.example.com重定向到http://example.com或以其他方式绕过http://example.com > http:// www .example.com 。
Usually one would use htaccess for this, but it should be a simple issue so I was wondering if there is a design-method to handle this? 通常,人们会为此使用htaccess,但这应该是一个简单的问题,所以我想知道是否存在一种设计方法来处理此问题?
To do that, you need to add PREPEND_WWW
setting to your settings.py
file. 为此,您需要将
PREPEND_WWW
设置添加到settings.py
文件中。
PREPEND_WWW = True # Default value is False
Django will then prepend the “www.”
subdomain to URLs that don't have it. 然后,Django将在
“www.”
子域之前添加没有该URL的URL。 So, URLs that lack a leading “www.”
will be redirected to the same URL with a leading “www.”
因此,缺少前导
“www.”
的URL将被重定向到带有前导“www.”
的相同URL “www.”
Note: This will work only if CommonMiddleware
is present in the MIDDLEWARE_CLASSES
settings. 注意:仅当
MIDDLEWARE_CLASSES
设置中存在CommonMiddleware
此方法才CommonMiddleware
。
MIDDLEWARE_CLASSES = (
...
'django.middleware.common.CommonMiddleware', # must be there in middlewares
...
)
You can do it only one-way in django, with PREPEND_WWW
: 您只能使用
PREPEND_WWW
在django中进行PREPEND_WWW
:
Whether to prepend the “www.” subdomain to URLs that don't have it.
是否在“ www。”子域名之前添加没有该域名的URL。 This is only used if CommonMiddleware is installed (see Middleware).
仅在安装了CommonMiddleware的情况下才使用此选项(请参阅Middleware)。 See also APPEND_SLASH.
另请参阅APPEND_SLASH。
However, you server should be configured correctly to recognize the URL and correctly route the request. 但是,应该正确配置服务器以识别URL并正确路由请求。 Otherwise you'll have issues (infinite redirects, for example).
否则,您将遇到问题(例如,无限重定向)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.