代码之家  ›  专栏  ›  技术社区  ›  Jake

如何将domain.com重定向到django下的www.domain.com?

  •  8
  • Jake  · 技术社区  · 15 年前

    如何重新定向domain.com/的所有请求…到 万维网 .domain.com/…在Django的网站上有301?

    显然,在urls.py中不能这样做,因为在那里只能得到url的路径部分。

    我不能在.htaccess中使用mod rewrite,因为.htaccess文件在django下不起作用(我想)。

    我猜是中间件还是ApacheConf?

    我用plesk在Linux服务器上运行django,使用mod wsgi

    6 回复  |  直到 6 年前
        1
  •  18
  •   Graham Dumpleton    15 年前

    有人指出,在配置方面,网络派系讨论是正确的,你只需要自己应用它,而不是通过控制面板。

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

    放入.htaccess文件中,或在适当的上下文中放入主apache配置中。如果在主apache配置中的virtual host中,您的servername为www.example.com,serverfalias为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,留给读者作为练习。-)

        2
  •  15
  •   Luper Rouch François Lagunas    15 年前

    这个 PREPEND_WWW 设置就是这样。

        3
  •  3
  •   maciek    6 年前

    a lightweight way to do that 涉及 VirtualHosts 和莫代利亚斯 Redirect directive . 您可以定义两个虚拟主机,一个用于重定向,另一个用于 site configuration :

    <VirtualHost *:80>
        ServerName example.com
        Redirect permanent / http://www.example.com/
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName www.example.com
        # real site configuration
    </VirtualHost>
    

    这将完成这项工作。

        4
  •  0
  •   damirault    15 年前

    这里有一个关于这个问题的完整线索 http://forum.webfaction.com/viewtopic.php?id=1516

        5
  •  0
  •   Taha Jahangir    9 年前

    这也可以通过中间件来实现。

    一些例子:

    这是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'\1://example.com/\3'),
        )
        """
        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))
    
        6
  •  0
  •   ptav    8 年前

    我尝试过Graham的解决方案,但无法使其正常工作(即使最简单的HTTP(而不是HTTPS)没有任何路径。我对WSGi没有经验,你可能猜得到,所有的帮助都非常感谢。

    这是我的尝试(从www.olddomain.com重定向到www.newdomain.com)。当我尝试部署它时,尝试访问www.olddomain.com会导致错误(“无法访问此页”):

    from django.core.wsgi import get_wsgi_application
    
    _application = get_wsgi_application()
    
    def application(environ, start_response):
      if environ['HTTP_HOST'][:21] == 'www.olddomain.com':
        start_response('301 Redirect', [('Location', 'http://www.newdomain.com/'),])
      return []
    
      return _application(environ, start_response)
    

    谢谢你的帮助