I want to catch from all urls like
example.com/<page_path> or example.com/<page_path>/
the <page_path> part. The variable could be empty (redirect to example.com/start) or a path to the desired page (most of the time a simple string, but can also have slashes in it)
This is my current urls.py which seems to work:
urlpatterns = [
url(r'^/$', RedirectView.as_view(url="start"), name='index'),
url(r'^$', RedirectView.as_view(url="start"), name='index'),
url(r'^(?P<page>.+)/$', views.PageView.as_view(), name='page'),
url(r'^(?P<page>.+)$', views.PageView.as_view(), name='page'),
]
But now I tried to simplify the expression and came up with this:
urlpatterns = [
url(r'^/?$', RedirectView.as_view(url="start"), name='index'),
url(r'^(?P<page>.+)/?$', views.PageView.as_view(), name='page'),
]
I want to capture everything except if there is a trailing slash.
Strange is, this works for
example.comexample.com/example.com/start
but not for
example.com/start/
Can someone point my error out?
page, so there is nothing left for the/?part to work onr'^/?$,r'^$'will work fine whether you typeexample.comorexample.com/.http://example.comandhttp://example.com/in your logs, you'll see"GET / HTTP/1.1", whether or not you include the slash. Django strips the leading slash from the request before resolving the url. So if you haver'^/$', it will actually match//, which isn't what you want. You don't need a leading slash inr'^$'in the same way as you don't put a leading slash inr'^(?P<page>.+)'. Django 1.9 will check your url patterns for this mistake, and a couple of others.