起初,感谢你的阅读:X
我正在通过linux apache和iptables把http流量从80端口的任何ip地址重定向到10.0.0.1:8080(django)。
当web用户向这个url发出请求时(hello.ggstatic.comgenerate_204******),我将路径处理为------。**generate_204**
url地址,通过这个规则。re_path('.*generate_204.*', lambda r: HttpResponseRedirect('splash/')),
.
Buuuuut, users are seeing `//splash/` instead of `/splash/` in browser.
`http://10.0.0.1:8080//splash/` #django 404 not found
users must see:
`http://10.0.0.1:8080/splash/` #django can handle it and via "include('splash.urls')"
问题:如何管理删除url地址栏中的"/"? 如何在url地址栏管理移动柄"/"!?
日志:我在django-project中的urls.py文件::
[09/May/2020 13:43:36] "GET //generate_204 HTTP/1.1" 302 0
Not Found: //splash/
[09/May/2020 13:43:36] "GET //splash/ HTTP/1.1" 404 2328
我的urls.py文件在django-project中。
urlpatterns = [
re_path('/static/', serve,{'document_root': settings.STATICFILES_DIRS}),
re_path('.*generate_204.*', lambda r: HttpResponseRedirect('splash/')),
path('', lambda r: HttpResponseRedirect('splash/')),
#path('admin/', admin.site.urls),
path('splash/', include('splash.urls')),
]
我的urls.py文件在django-application中。
urlpatterns = [
re_path('^$', views.start, name='start'),
re_path('index/$', views.start, name='start'),
path('validation', views.validation, name='validation'),
path('bye', views.goodbye, name='goodbye'),
]
我编辑了我的问题。
path('splash/', include('splash.urls')),
#above line failed to handle //splash behavior.
to:
re_path('.*splash/', include('splash.urls')),
#above line works. but it's not ok. and we can have everything here between ip/domin:8080 and /splash: http://10.0.0.1/******/splash/. and this behavior is bad for url handling.
该 CommonMiddleware
处理设置的重定向 PREPEND_WWW
和 APPEND_SLASH
.
所以这和你想要的用法差不多。你收到一个带有特定url的请求,想把它重定向到另一个(或者换句话说:纠正url)。
因此,一个简短的(虚)的例子与所述中间件的一点代码。
class RedirectDoubleSlashMiddleware(MiddlewareMixin):
response_redirect_class = HttpResponsePermanentRedirect
def process_request(self, request):
uri = request.build_absolute_uri().replace('http://', '').replace('https://', '')
possible_uri = uri.replace('//', '/')
if uri != possible_uri:
return self.response_redirect_class('{}://{}'.format(request.scheme, possible_uri))