I've set the APPEND_SLASHES directive to False in my settings.py file and yet Django carries on to redirect some (but not all) requests which is incredibly annoying. What could be causing this issue?
Basically, if I make a request without the slash, it will return the correct response body, but with a redirect (301), redirecting it to the same URL but with a slash at the end which will not match because it shouldn't end in a slash.
Is there a fix for that or do I just need to strip the slash redirection code from CommonMiddleware?
Edit: CommonMiddleware is not the culprit. The URL reaches it with a slash at the end.
Edit2: Only happens in Firefox. What the hell? Firebug registers two HTTP requests, one of which is a redirect. Only one request actually hits the server, and it's the one with the slash at the end. The 301 appears out of nowhere and isn't even sent (Wireshark doesn't register it).
301 are permament redirects. So if you have had this option set to true before firefox will still remember this permament redirect and go to page with slash appended. Try to clear firefoxes caches and offline contents.
Related
Is it possible to avoid url redirections to url/ in Django/DjangoRestFramework? My point is that some frontend developers use url without trailing slashes and Django redirects such requests to url/ which slows down the API.
Adding
APPEND_SLASH = False
to settings doesn't solve the problem as it returns 404 responses.
I need to redirect this URL (http://www.example.com/learn) to this URL (http://www.example.com/learn-it).
Problem is that it matches the rule on the redirected URL, and makes an infinite loop.
This does not work:
Redirect 301 http://www.example.com/learn http://www.example.com/learn-it
.htaccess 301 Redirect
The smoothest way to redirect your visitors is to use an .htaccess redirect. This has no delay since before a page is served to the browser the server checks first for an .htaccess file... if it sees this the old page never loads, instead visitors are sent directly to the new page.
These are a few .htaccess redirect codes that I've used that might come in handy for you. This is not a complete list by any means, but it took me ages to find how to do these so I'll save you the hassle and list them here. Oh, and please don't email me with questions about how these work, like I said, I found these with the help of others.. I have no idea in the slightest how to write this stuff and take no credit (or responsibility) for how they work.
If you're more technically minded than I am and want the information straight from the source, check the Apache Tutorial: .htaccess files for more detailed info.
Important notes about htaccess redirection
Always be sure to upload .htaccess files in ascii mode, sending it up as binary will break it (and usually make your server very, very unhappy.)
.htaccess does not work if you're on a windows server.
Make sure you triple check your changes. Clear your cache and look, test the server headers to make sure you see a 301 (that means its permanent) not a 302 (temporary) unless you are absolutely sure you really mean temporary.
Since some operating systems don't allow you to make a file without something before the "." you may need to save it as something.htaccess, some may even have to save it as htaccess.txt and change it once you've uploaded it.
Make sure your ftp program will show .htaccess files (FileZilla does and is free) It is a bit hard to edit something you can't see ;)
Double check that you're not overwriting an old one (some servers already place one there for your custom 404 pages etc.)
Make sure you replace example.com with your own sites URL ;-)
To Move a single page
Quick, easy and seamless for your visitors.
Redirect 301 /oldpage.html http://www.example.com/newpage.html
To Move an entire site
This will catch any traffic on your old site and redirect it to your index page on your new server. If you want to redirect each page to its new spot, this isn't the one for you.
Redirect 301 / http://www.example.com/
For detail explanation. How to redirect page usin .htaccess . read this
You should use RedirectMatch in order to use regex to be able to match exact URI:
RedirectMatch 301 ^/learn/?$ /learn-it
Make sure to clear your browser cache before testing this.
I have my Django app. I have a redirect URL(say a 404 page) to be redirected when no other URL matches. Now if any url is called as
mysite.com/something
I am redirected to the 404 page. But
mysite/something/
works fine.
The redirection url added to the end of all:
url(r'^.*/',theview),
When I remove the redirect url from the urls.py, the problem is cleared and the above URL works (without / at the end). Why is the error?
First of all, it would be a good idea to link to your previous post and mention you are using a hack that I gave you, because (A) it's not normal setup and (B) Someone might come up with a better idea than mine
Secondly, you're seeing this behaviour because of normal url processing. See, the urls mysite.com/something and mysite.com/something/ are not the same. To match it with django's urls, the difference would be:
url(r'^something/$')
url(r'^something$')
Since the difference is so minor, when using a normal setup, after failing to find the a url without a forward slash django's common middlewere* will automatically try to add one and test it. It's only then that it would give up and forward you to a 404 page.
However, in your setup, the catch-all url prevents the second round because it does apply to the url without the forward slash. My solution? Don't worry about it. The only reason you're using this hack is because Debug=True means a debug page instead of your custom 404 page, a problem you won't be facing when moving to a production environment
*and a big thanks to #Alasdair who pointed this out in the comments
I'm trying to do some URL redirects and the redirects are working properly, however if I try to redirect a URL that ends in a forward slash the redirect does not work. For example, I'd like to redirect http://mydomain/foo/ to /bar/ does not work. However http://mydomain/foo to /bar/ does redirect properly. Can anyone tell me how to redirect urls with training forward slashes properly?
I think that if a web server indicates a trailing slash, it immediately looks for the default file (e.g. index.html) in the given directory.
This convention is speeding up the web server work.
e.g.
http://mysite/test/ ==> is a directory
http://mystite/test ==> is a file
Our website url is ending with slash "/", it get encoded to "%2F". because of this slash facebook like button is not showing on the website.
Eg
Not working url: because in href parameter url has "%2F", but if i remove the "%2f", it starts working. Earlier it was working fine.
https://www.facebook.com/plugins/like.php?action=like&channel_url=https%3A%2F%2Fs-static.ak.facebook.com%2Fconnect%2Fxd_arbiter.php%3Fversion%3D9%23cb%3Df3e1268db4%26origin%3Dhttps%253A%252F%252Fwww.bcgperspectives.com%252Ff189b4c84c%26domain%3Dwww.bcgperspectives.com%26relation%3Dparent.parent&extended_social_context=false&font=arial&href=https%3A%2F%2Fwww.bcgperspectives.com%2Fcontent%2Fvideos%2Fleadership_paul_deighton_organizing_london_2012_olympics%2F&layout=button_count&locale=en_US&node_type=link&ref=.UCfPf5lBPRI.like&sdk=joey&show_faces=false&width=90
any solution for it.
I can’t get a like button shown on https://developers.facebook.com/docs/reference/plugins/like/ when I input your URL https://www.bcgperspectives.com/content/videos/leadership_paul_deighton_organizing_london_2012_olympics/, no matter whether the trailing slash is there or not.
However, it does get shown if I use http:// instead of https:// in front of your URL – then it works for both versions, the one with the trailing slash and the one without.
An interesting fact though is, that I can not see any information scraped from your page using the debug tool on your URL – doesn’t matter if its the slash or no-slash version or the HTTP vs the HTTPS version, since your server redirects to the HTTPS version with the trailing slash anyway. So that might point to some problem FB’s scraper has with your URL/domain (although no explicit error messages are shown).