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.
Related
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.
Is it possible to redirect multiple urls by keeping a part of the url or actually removing a part?
For example if I have a site with city guides for multiple cities with the url loremipsum.com/guide-seattle and I change all the urls to loremipsum.com/seattle
So I would want to create a redirect / rewrite rule with maybe regex saying if url is /guide-randomcityname/ then remove guide- and keep randomcityname and redirect to /randomcityname/
A general rule for whatever the city name could be.
Thanks
You can use this redirect rule to remove /guide- from start of your URLs:
RedirectMatch 301 ^/guide-([^/]+/?)$ /$1
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.
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.
I have a website, it contains many urls, like:
/public
/public/images/...
/questions/...
/answers/...
...
I want to add some cache headers to all the urls except `/public/.*'. But I don't know how to write this regex.
I tried:
^(?<!\Q/public/E)/.*
But it not works.
.htaccess under apache? try:
<LocationMatch "^(?!/public/)[^\.]+$">
Header set Cache-Control max-age=7200
</LocationMatch>