Django URL with catch all except /admin and /blog - django

I want to make a catch all URL in my Django project...
It works fine, but now i can't go to my /admin and /blog page .. any idea how I can do this?..
My urls.py
url(r'^admin/', include(admin.site.urls)),
(r'^blog/$', 'apps.blog.views.index'),
(r'^([a-zA-Z0-9\-\_]*)', 'apps.review.views.show_search'),

You don't need to handle this manually with a RedirectView. You likely need to add the CommonMiddleware to your MIDDLEWARE_CLASSES settings if it is not already in there.
(inserting 'django.middleware.common.CommonMiddleware' into that list or tuple). It usually goes near the top of that declaration. Its documentation is here:
https://docs.djangoproject.com/en/1.5/ref/middleware/#django.middleware.common.CommonMiddleware
You also need to be sure APPEND_SLASH is set to True in your settings.
Furthermore, your last pattern is likely to match anything, which I suspect could be the real culprit of your problem. A URL without a trailing slash has to fail to match any patterns in order for it to automatically redirect to one with a trailing slash per that middleware. I think you can fix the last URL pattern by appending /$ to that pattern

The admin and blog urls have trailing slashes i.e. /admin/ and /blog/. The urls with the trailing slashes should work at the moment.
If you want to make the urls work without the slash, you could add entries for /admin and /blog, and use RedirectView to redirect.

Related

Exclude specific directory from htaccess redirectmatch

I am migrating my blog from a subdirectory to the root of my domain, but I am leaving the admin panel in its current location, as such I need to do a 301 redirect for anything which matches the following path, except the "admin/" subdirectory.
I have the core redirect working with this:
RedirectMatch 301 (^/oldblogpath/)(.*) http://www.example.com/$2
The path I want to exclude from the above is "/oldblogpath/admin/" - please help me understand what I'm missing!
You can use a negative lookahead pattern for this:
RedirectMatch 301 ^/oldblogpath/(?!admin/)(.*) http://www.example.com/$1
(?!admin/) is a negative lookahead condition that fails the match of admin/ appears right after matching starting directory path.
Make sure to clear old browser cache completely before testing this change.

why its showing empty path didnt match any of these

Using the URLconf defined in My_Ecom_Project.urls, Django tried these URL patterns, in this order:
admin/
account/
^static/(?P.)$
^media/(?P.)$
The empty path didn't match any of these.
Add a path with the urlpattern being '/'. You can also use '' if your webserver handles stripping final slashes. As #Willem Van Onsem mentions you're simply not handling the path you're webserver is passing to Django.

Django exclude a single url from adding slash

I've added 2 views to my Django urls config for SEO but when I view them on my site it redirects them to the url I setup but with an added slash. I know Django likes to tidy up URLs with a slash (and I like this) but is there a way to exclude a single url or a few urls from this feature?
...
url(r'^robots\.txt/$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
url(r'^sitemap\.xml/$', TemplateView.as_view(template_name='sitemap.xml', content_type='text/xml')),
...
These append slash to the urls resulting in robots.txt/ and sitemap.xml/ not robots.txt and sitemap.xml
If you don't want slashes at the end of robots.txt and sitemap.xml, then simply remove the trailing slashes from those regexes.
url(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
url(r'^sitemap\.xml$', TemplateView.as_view(template_name='sitemap.xml', content_type='text/xml')),
The URLs /robots.txt and /sitemap.xml will then match, and Django will not append the slash.

rewrite any combination of index.html

I am interested in URL redirect of any letter case combination of index.html to all lowercase of index.html.
ie:
/foo/bar/INDEX.html
to
/foo/bar/index.html
or
/hello/world/funk/indeX.HTML
to
/hello/word/fund/index.html
I have tried couple regex but no luck. I am interested in Redirect only if there are any uppercase(s) in index.html
/hello/there/index.html
should not redirect anywhere.
I have access to httpd.conf hence I am using RewriteMap lc int:tolower
Try this: (?!index\.html)(?i)index\.html(?-i) it first checks if the string is not index.html, and then matches any string that is case insensitive index.html. Try it here: https://regex101.com/r/GNhAwG/1

Django greedy characters missing in reverse urls

I am trying to use the following rule to map urls in django
url(r'^(?P<permalink>[a-zA-Z0-9_-]*)/?$', views.page, name='page'),
This should match pages like
site.com
site.com/super-awesome-page/
This works however the reverse urls provided by the url template tag are missing the trailing / i.e. "site.com/page" these do get captured pattern but I want my links to show up in my page correctly how can I get this to work correctly.
I would have expected since the trailing slash is greedy it should be included in the reverse url.
The trailing slash is optional in your regex, so django doesn't generate it for you for the reverse URL.
The easiest solution is probably to make the trailing slash non-optional. With the default settings, django will redirect the non-slash version to the slash-version. Easiest to just standardize on that.