I have a django app that has / at the end of every URL conf. Example:
# user home page
(r'^home/$', 'user_home_page'),
However, I'm noticing this is causing a ton of redirects on my server, because when people dont add the /, it redirects them. Is there any way to have it accept both without a redirect except doing:
# user home page
(r'^home$', 'user_home_page'),
(r'^home/$', 'user_home_page'),
or should I avoid URL confs like that?
While you can accept both without doing a redirect by using:
(r'^home/?$', 'user_home_page'),
It is not best SEO practice because it will look like you have duplicate content and your hits will be split between the two pages.
You could modify your APPEND_SLASH Django setting, I haven't used it before.
You may be wondering what happens if someone requests the URL /hello
(that is, without a trailing slash). Because our URLpattern requires a
trailing slash, that URL would not match. However, by default, any
request to a URL that doesn’t match a URLpattern and doesn’t end with
a slash will be redirected to the same URL with a trailing slash.
(This is regulated by the APPEND_SLASH Django setting, which is
covered in Appendix D.)
This was taken from http://djangobook.com/en/2.0/chapter03/
The patterns are Python regular expressions. How about:
(r'^home(/?)$', 'user_home_page'),
should I avoid URL confs like that?
Yes. Django already handles this for you.
See this: https://docs.djangoproject.com/en/1.3/ref/middleware/#module-django.middleware.common
If APPEND_SLASH is True and the initial URL doesn’t end with a slash,
and it is not found in the URLconf, then a new URL is formed by
appending a slash at the end. If this new URL is found in the URLconf,
then Django redirects the request to this new URL. Otherwise, the
initial URL is processed as usual.
Use Django REMOVE_SLASH middleware instead.
https://gist.github.com/2204099
https://gist.github.com/gists/2204099/download
You need to ensure your application uses / consistently, make a choice which to use, and 301 redirect the rogue variation to the correct canonical one.
There should only be one version of a page, and the correct solution is in most cases to 301 redirect to the canonical version.
In some situations with additional paramenters for tracking you should also define a canonical in the head.
By using consistent URLs within your APP and out on the web in links pointing back to your app, you then won't have lots of redirects.
Related
I use django-tenant-schemas and I need to change the way it selects the current tenant:
from https:// tenant1.mydomain.com/
to https:// mydomain.com/tenant1/
To do that I've coded a middleware, it's quite simple and it's working, the tenant is selected. The problem comes in the url resolution in django. It raises a 404 cause there is no url mapping that includes that suffix "tenant1".
How could I tell django that the urls now have a dinamic suffix that changes deppending on the tenant? This should be used reversing and generating urls.
I need to redirect my clients to another endpoint in my django app. I know I can use relative urls with request.build_absolute_uri() to do this, but I am searching for a generic solution that doesn't require the redirecting handler to know its own place in the URL hierarchy.
As an example, I have handlers at the following two URLs:
https://example.com/some/other/namespace/MY_APP/endpoint_one
https://example.com/some/other/namespace/MY_APP/foo/bar/endpoint_two
Both handlers need to redirect to this URL:
https://example.com/some/other/namespace/MY_APP/baz/destination_endpoint
I would like for endpoint_one and endpoint_two to both be able to use the exact same logic to redirect to destination_endpoint.
My app has no knowledge of the /some/other/namespaces/ part of the URL, and that part of the URL can change depending on the deployment (or might not be there at all in a development environment).
I know I could use different relative urls from each endpoint, and redirect to the destination URL. However, that required that the handlers for endpoint_one and endpoint_two know their relative position in the URL hierarchy, which is something I am trying to avoid.
After doing more research and talking with coworkers, I realized that reverse does exactly what I need.
Is it possible redirect to another web after login with python-social-auth?
let's say:
The example above is according to the documentation and it is working well, but when I try this:
I get the following error:
http://example.com:8000/accounts/profile/ Not Found
This makes sense; I don't have this URL defined
At this point I have been already logged in, but I see an error page.
But with the same configuration, if I redirect to my own site, this works, I think this is something about settings, but I don't know which one.
EDIT:
If I remove the next GET param in the first configuration, it raise the same error.
It isn't safe to redirect to a different domain, e.g. https://www.google.com. To understand why, imagine that I send your users a link to:
http://www.yoursite.com/login?next=http://myevilsite.com
If your login page trusted the next url, then your users will be redirected to my site after they have logged in. I could use this for phishing attacks.
To prevent against this, Django checks that the next url is safe to redirect to. If it is not safe, then it will redirect to the settings.LOGIN_REDIRECT_URL, which defaults to /accounts/profile/. You can see the code here.
I had the same problem and I think there is some situations where it is interesting to redirect to another domain (ie: you have your backend under a different domain than your frontend).
After some investigation I found that you can control which domains are safe to redirect to (https://github.com/python-social-auth/social-core/blob/1d809941ab8a99af9e1bdf12bae548202c94eaa2/social_core/actions.py#L20).
Just add this to your settings.py:
SOCIAL_AUTH_ALLOWED_REDIRECT_HOSTS = ['domain1', 'domain2']
I have this url in my urls.py:
url(r'^showrooms/', include('apps.showrooms.urls')),
and i want to have another url like this(i.e w/o 's' of showrooms)
url(r'^showroom/', include('apps.showrooms.urls')),
but having two urls like above results in two urls mapping to same data which is not good from seo perspective.So the solution is to redirect showroom/ urls to showrooms/ urls, but how do i get to this as showrooms/ is pointing to set of urls in apps folder ?
If you want to keep it close to django you can use the redirect shortcut as found in the documentation here: https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect
Otherwise this is more like a webserver question, for apache there is mod_rewrite in which you can 301 redirect, and nginx has the rewrite directive which should suit your needs just fine!
I am new to Django, and i am now trying to use the HttpResponseRedirect() function. But I am so confused that if I use it like HttpResponseRedirect('good/'), and the current page is '/bad/', it can only be redirected to '/bad/good/', which is an url of current page appended with the url value from the HttpResponseRedirect() function. I tried to search google, and could not find any solution.
How can I redirect to the page with specific url? For example, HttpResponseRedirect('/good/') to /good/ rather than /bad/good/ ?
Surely you must see that there's a difference between 'good/' and '/good/'? The former will always add itself onto the existing page, whereas the latter will start from the root. This is basic web behaviour, and nothing to do with Django.
In any case, you should never hard-code URLs like that, but should use Django's URL-reversing functionality to calculate the URLs dynamically.
If you want to redirect to urls in your domain, you can use redirect. You can use redirect to view with by using patterns in your urls.py file or to any urls you 'd like. Although I strongly encourage the usage of views as indicates the different tutorials available on their website.
Better check out django documentation that is one of the most complete out there (to my humble opinion)
/edit for lack of clarity indeed.