2016-11-29 1 views
0

이 사이트는 http://example.com/ 'somepath'와 같이 다른 URL을 생성합니다. 내가하고 싶은 일은 http://example.com/ 'somepath'에서 http://www.example.com/ 'somepath'로 사용자를 리디렉션하는 것입니다. 내가 알기로 django-hosts를 사용하는 것이 가능합니다.
그것은 내가 settings.py에서 다음 한 지침 말했다 것 같이 hostsconf/URL을에서Django-hosts가 www가 아닌 ​​www로 리디렉션합니다.

ALLOWED_HOSTS = ['www.example.com', 'example.com'] 

INSTALLED_APPS = [ 
    ..., 
    'django_hosts', 
] 

MIDDLEWARE = [ 
    'django_hosts.middleware.HostsRequestMiddleware', 
    ... 
    'django_hosts.middleware.HostsResponseMiddleware', 
] 

ROOT_URLCONF = 'appname.urls' 
ROOT_HOSTCONF = 'appname.hosts' 
DEFAULT_HOST = 'www' 
DEFAULT_REDIRECT_URL = "http://www.example.com" 
PARENT_HOST = "example.com" 

: hostsconf에서

from django.conf.urls import url 
from .views import wildcard_redirect 

urlpatterns = [ 
    url(r'^(?P<path>.*)', wildcard_redirect), 
] 

/조회수 :

from django.conf import settings 
from django.http import HttpResponseRedirect 

DEFAULT_REDIRECT_URL = getattr(settings, "DEFAULT_REDIRECT_URL", "http://www.example.com") 

def wildcard_redirect(request, path=None): 
    new_url = DEFAULT_REDIRECT_URL 
    if path is not None: 
     new_url = DEFAULT_REDIRECT_URL + "/" + path 
    return HttpResponseRedirect(new_url) 

그러나처럼 보인다 http://example.com/ 'somepath'로 이동하면 "400 Bad Request"가 반환되고 http://www.example.com/ 'somepath'는 올바른 d를 가리키기 때문에 작동하지 않습니다. estination. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변

1
+0

이미를 시도, 같은 일 – Steve

+1

당신이 얻을 무엇? 400, ALLOWED_HOSTS를 설정하지 않으면 얻을 수 있습니다. show trace back P. django-hosts를 제거했다면 그는 – dd42

+0

로 리다이렉트하고 싶지 않았다. allowed_hosts를 설정하고 PREPEND_WWW가 "400 Bad Request"와 같은 에러를 리턴한다. – Steve

관련 문제