Is it Possible to Handle Sub-domains in Django without having each of them registered with registrar? - django

Is it possible to handle sub-domains in Django without having each of them registered with registrar, provided i have purchased and setup the domain?
Suppose i own example.com. I have purchased it, and setup it to point to my website at some hosting service. But, did not add a sub-domain blog.example.com. Then, can i serve some webpage at blog.example.com?
How request.subdomain in django-subdomains (doc) library is supposed to be used?

After all, I found an answer.
Instead of registering each subdomain separately with the registrar, We should register a wildcard DNS entry for the domain so that all expected subdomain will point to that one. Then configure the web server to catch all subdomains. An example I got for nginx is:
server {
server_name example.com www.example.com;
root www/pub;
}
server {
server_name ~^(.*)\.example\.com$ ;
root www/pub/$1;
}
Then using django-subdomains library (doc) we can get the sub domain from the views or middlewares. Based on that we can redirect the view to wherever wanted using django. This is the vague idea. Anyone experienced please elaborate. An example, http://sarahah.com gives a sub domain for every user. This is where I found this is possible. But don't know how they implement it.
A discussion is found in this reddit page.

Related

What is the correct way to do a redirection from www. to the naked domain (Server or App or Domain Provider)?

I currently have an app that works with both www.domain.com and domain.com. Considering that this seems to be bad for SEO, I want it to always redirect to the naked domain.
My app works in this way:
Server: Google Cloud Platform (App engine)
App: Django
Domain provider: Godaddy
As I have researched the redirection can be done from any of these 3 options. So I want to ask: What is the best option and why?
I tested to do the redirection with GoDaddy on App Engine (An A record for www pointing to #) but when I set my custom domain in App Engine, I noticed that for the www.domain a C Record pointed to ghs.googlehosted.com setting was required in the app engine panel, I omitted that in order to do the redirection in GoDaddy, but the app wouldn't load when visiting from www.domain, a 404 error appeared (Also tried by omitting the addition of the www.domain altogether in app engine settings) So I wasn't able to accomplish this in the same fashion than an app that is hosted in a single server.
I think the easiest way would be to handle this inside the app itself like this site mentions

Redirect domain to .com in django in url.py

I have a website in Django, hosted in Heroku.
I have 2 domains, registered in different places:
mysite.com and mysite.com.br
I don't want to be penalized by Google for having 2 domains with the same website, therefor I would like to redirect everyone who enters mysite.com.br to mysite.com.
I entered in the DNS mysite.com.br(Not hosted anywhere) CNAME mysite.com(hosted in Heroku), which makes the user actually access the Heroku content, but the url keeps the .BR ....
So the Heroku support told me to do the redirection in the application. In this case, what's the best practice to redirect? I would imagine to do that in the url.py, but how can I do that if the "path" doesnt read the domain?
Thanks.
You can't do this in the URLs. I would write some middleware to check the host via request.get_host and redirect if you're not on the canonical one.
In fact it looks like someone has already written this: django-enforce-host.

Use GitHub Pages domain for a Netlify site

I would like to build a Netlify (or perhaps a Zeit Now) site.
Despite the extra feaures of Netlify and Zeit, I find GitHub pages foo.github.io domain name more catchy than foo.netlify.com.
I see that GitHub allows to use a custom domain, so that you can point your example.com domain to your GitHub pages.
Is the reverse possible? That is, I'd like using foo.github.io as the domain name for the site stored at foo.netlify.com.
Because you do now own the domain github.io (and by extension, the domain foo.github.io) this is not feasible.
To add a custom domain to Netlify, you need to point a CNAME to your-random-string-url.netlify.com (via DNS).
GitHub allows you to redirect a domain, but this will rewrite the url in the browser - which is not what you want.
I would recommend investing in your own domain name, and point it to whichever service you end up using. You will then not be locked into any service in the future.

DNS hosting public and web application on different hosts

Here is my setup.
Public site hosted by squarespace.com (www.example-domain.com)
Web application (AWS EC2/ELB), i would like to be available via the same domain. (my.example-domain.com)
Custom profile pages available as www.example-domain.com/username
My question is how can i setup the DNS to achieve this? If can't do it just through DNS, any suggestions? The problem i am facing is that if squarespace.com is handling the www.example-domain.com traffic how can i have it only partially handle it for certain urls. Maybe i am going about this in the wrong was all together though.
The two first are ok. As you mention, (1) is not compatible with (3) for a pure DNS config as www of example-domain.com has to be configured to a single end-point.
Some ideas of non-DNS workaround:
Having the squarespace.com domain on sqsp.example-domain.com and configure your www domain to a custom web server on which you configure the root (/) to redirect (HTTP 300) to sqsp.example-domain.com. It will be quite transparent for the user, except in his browser address.
The same but setting on / a full page HTML iframe containing sqsp.example-domain.com.
The iframe approach is a "less clean", Google the solutions to build your opinion.
EDIT:
As #mike-ryan mentioned, there is the proxy solution as well where you configure you web server to request another server to get the content to return to your user. If you are already using AWS, a smart way to do this is to use CloudFront: you can setup CloudFront to proxy one server on one URL and proxy another server on other URL. Actually, this is maybe the faster to way to implement you need. Of course, a proxy is one more "hop", so it may add more delay.
If you really want to have content served from different servers while only using a single domain name, you'll need to set up a proxy server to handle the request routing for you. I am assuming your custom profile pages must be served from your EC2 instance.
Nginx will receive all requests, and will then decide whether they should be sent to Square Space or your web app. Requests will be reverse proxied to Square Space or to your app, depending on the URL.
This is similar to #smad's answer, except it will all be invisible to the users which IMHO is better than redirecting the user to a new domain name.
Example steps:
Set up an Nginx server, create two virtual hosts - one for my.example.com, and one for www.example.com
Create two upstreams in your Nginx config - one for Square Space, and one for your app
Configure the www.example.com virtual host to reverse proxy connections to the Square Space upstream, if the URL is "/". Otherwise, traffic should be proxied to your app upstream [0]
Configure the my.example.com virtual host to proxy all traffic to your app upstream
[0] how to reverse proxy via nginx a specific url?

Mapping a naked URL to the equivalent www URL

I'm currently using dotcloud hosting for an application and I'm redirecting my naked url using my domain registrar namecheap (through "#" hostname and "*" hostname as a URL direct- and then a cname for www to dotcloud)
This works well for re-directing people back to main www.
My issue is I want to map the visitor to the equivalent naked domain url they were trying to visit. E.g. http://example.com/websitepage1 to http://www.example.com/websitepage1. Right now visiting any page on the site minus the "www" will just redirect you to the homepage.
Any ideas or suggestions?? I'd prefer not to switch out of dotcloud hosting for the moment but will do so if it's impossible to get this to work another way.
Thanks!
UPDATE
The urls that need to be redirected are created dynamically so it's not impossible to hard code them. The DNS doesn't have to be namecheap. I can put the nameservers else where if there is an option that allows redirect of naked urls to the www equivalent. Any ideas appreciated!
You need to add rewrite rules to nginx.conf:
server {
server_name hostname.org;
rewrite ^(.*) http://www.hostname.org$1 permanent;
}
You can specify custom rewrite rules (and actually almost any Nginx setting) by creating a file named nginx.conf at the top of your app. http://docs.dotcloud.com/0.9/guides/nginx/
Given their documentation (0), it seems possible to do the following 301/302 redirects using Namecheap's forwarding options:
Setup a naked domain redirect from example.com to www.example.com
Setup a naked domain redirect with a url from example.com/url/ towww.example.com/url/`
I don't see in the documentation where it says they support a wildcard option, so for every url you'd like to support, it seems you may have to create a forwarding entry.
(0): http://www.namecheap.com/support/knowledgebase/article.aspx/385/77/how-do-i-setup-url-forwarding-for-a-domain