我该如何重定向domain.com到WWW.domain.com Django的下? [英] How do I redirect domain.com to WWW.domain.com under Django?

查看:119
本文介绍了我该如何重定向domain.com到WWW.domain.com Django的下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何去在Django网站重定向为domain.com / ...所有的请求的 WWW .domain.com / ...和301?

How do I go about redirecting all requests for domain.com/... to www.domain.com/... with a 301 in a django site?

显然,这无法在urls.py做,因为你只得到了URL的路径部分在那里。

Obviously this can't be done in urls.py because you only get the path part of the URL in there.

我不能在.htaccess中使用mod-rewrite,因为.htaccess文件做的Django下什么(我认为)。

I can't use mod rewrite in .htaccess, because .htaccess files do nothing under Django (I think).

我猜中间件或Apache的conf什么?

I'm guessing something in middleware or apache conf?

我正在Django的Linux服务器使用Plesk上,使用MOD WSGI

I'm running Django on a Linux server with Plesk, using mod WSGI

推荐答案

该WebFaction讨论有人指出的是正确的尽可能的配置,你就必须自己运用它,而不是通过一个控制面板。

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.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

放入.htaccess文件,或者在适当的范围内主要的Apache配置。如果主要的Apache配置虚拟主机里面,你将有服务器名是www.example.com和ServerAlias​​为example.com,以确保虚拟主机处理这两个请求。

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.

如果你没有访问任何Apache配置,如果需要的话,可以使用各地的Django WSGI应用程序入口点一个WSGI包装完成。是这样的:

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:

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就留给读者自己练习。 : - )

Fixing this up to include the URL within the site and dealing with https is left as an exercise for the reader. :-)

这篇关于我该如何重定向domain.com到WWW.domain.com Django的下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆