apache 如何在 Django 下将 domain.com 重定向到 WWW.domain.com?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2285879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I redirect domain.com to WWW.domain.com under Django?
提问by Jake
How do I go about redirecting all requests for domain.com/... to www.domain.com/... with a 301 in a django site?
我如何在 Django 站点中使用 301将 domain.com/... 的所有请求重定向到www.domain.com/... ?
Obviously this can't be done in urls.py because you only get the path part of the URL in there.
显然这不能在 urls.py 中完成,因为你只能在那里获得 URL 的路径部分。
I can't use mod rewrite in .htaccess, because .htaccess files do nothing under Django (I think).
我不能在 .htaccess 中使用 mod rewrite,因为 .htaccess 文件在 Django 下没有任何作用(我认为)。
I'm guessing something in middleware or apache conf?
我在猜测中间件或 apache conf 中有什么?
I'm running Django on a Linux server with Plesk, using mod WSGI
我在带有 Plesk 的 Linux 服务器上运行 Django,使用 mod WSGI
回答by Graham Dumpleton
The WebFaction discussion someone pointed out is correct as far as the configuration, you just have to apply it yourself rather than through a control panel.
有人指出的 WebFaction 讨论就配置而言是正确的,您只需要自己应用它而不是通过控制面板。
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/ [R=301,L]
Put in .htaccess file, or in main Apache configuration in appropriate context. If inside of a VirtualHost in main Apache configuration, your would have ServerName be www.example.com and ServerAlias be example.com to ensure that virtual host handled both requests.
放在 .htaccess 文件中,或者在适当的上下文中放在主 Apache 配置中。如果在主要 Apache 配置中的 VirtualHost 内部,则您的 ServerName 为 www.example.com,ServerAlias 为 example.com,以确保虚拟主机处理这两个请求。
If you don't have access to any Apache configuration, if need be, it can be done using a WSGI wrapper around the Django WSGI application entry point. Something like:
如果您无权访问任何 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)
Fixing this up to include the URL within the site and dealing with https is left as an exercise for the reader. :-)
解决这个问题以在站点中包含 URL 并处理 https 留给读者作为练习。:-)
回答by Luper Rouch
The PREPEND_WWWsetting does just that.
该PREPEND_WWW设置就是这样做的。
回答by maciek
There is a lightweight way to do thatinvolving VirtualHostsand mod_alias Redirect directive. You can define two VirtualHosts, one holding the redirect and another holding the site configuration:
有一种轻量级的方法可以做到这一点,涉及VirtualHosts和 mod_alias Redirect 指令。您可以定义两个 VirtualHost,一个保存重定向,另一个保存站点配置:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
# real site configuration
</VirtualHost>
And that will do the job.
这将完成这项工作。
回答by Taha Jahangir
This also can be done with a middleware.
这也可以通过中间件来完成。
Some examples:
一些例子:
- http://eikke.com/django-domain-redirect-middleware/
- https://djangosnippets.org/snippets/510/
- https://djangosnippets.org/snippets/434/
- http://eikke.com/django-domain-redirect-middleware/
- https://djangosnippets.org/snippets/510/
- https://djangosnippets.org/snippets/434/
This is a better version of snippet-510:
这是snippet-510的更好版本:
class UrlRedirectMiddleware(object):
"""
This middleware lets you match a specific url and redirect the request to a
new url. You keep a tuple of (regex pattern, redirect) tuples on your site
settings, example:
URL_REDIRECTS = (
(r'(https?)://(www\.)?sample\.com/(.*)$', r'://example.com/'),
)
"""
def process_request(self, request):
full_url = request.build_absolute_uri()
for url_pattern, redirect in settings.URL_REDIRECTS:
match = re.match(url_pattern, full_url)
if match:
return HttpResponsePermanentRedirect(match.expand(redirect))

