Mapping a naked URL to the equivalent www URL - django

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

Related

Regex - redirect from website.com to website.com/us and append substring

Can anyone help me correct my regex?
My hosting provides IP geolocation and I want to redirect US clients to another sub-directory of my website.
The website runs on a Nginx server and the hosting provides an interface where one can add redirection rules. The interface consists of Domain, Redirect from, Redirect To, Traffic from (country) and HTTP status code 301 or 302.
Example:
for all non-US clients website.com/blog/article/really-good-book
only for US clients website.com/us/blog/article/really-good-book
I currently have:
Redirect from ^/(?!us/)(.*)$
Redirect to /us/$1
This currently redirects me to website.com/us/index.php and nothing else. So the redirect is applied, it only appends index.php instead of blog/article/really-good-book.
Thank you for your help.
Eventually I found my own solution:
Redirect from ^(?!(/us|/index.php|/wp-admin|/wp-login.php))(.*)$
Redirect to /us/$1
I also added a rule for my wordpress backend, otherwise it would always redirect me back to website.com/us while trying to access website.com/us/wp-admin.

AWS Application Load Balancer redirect all www and non http to https://

I have a rule setup to redirect all traffic from http to https
How can I also redirect http://www to https://? As far as I can tell the {host} variable contains www and I cant remove it.
In fact it's pretty simple
Instead of writing {host} in your rule just put the name of your website.
So, for example, it will look like this :
https://example.com:443/#{path}?#{query}

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

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.

Can I set up a CNAME catch-all redirect from one subdomain level to a higher subdomain?

I have hundreds of subdomains set up for environments in Route53 for AWS that look like this:
<appX>.dev.internalurl.us
<appX>.qa.internalurl.us
<appX>.pt.internalurl.us
<appX>.internalurl.us
The issue is that our production internal urls are missing the 'prod' env in the url which requires us to add conditionals to all our config scripts, like:
if 'prod.' in url:
url = url.substring('prod.', '')
What I'd like is:
<appX>.prod.internalurl.us to go to <appX>.internalurl.us automatically.
EDIT:
I added a CNAME to route prod.internalurl.us to internalurl.us like so:
*.prod.internalurl.us > internalurl.us
but this obviously won't work since I need a capture group on the wildcard! It's ignoring the first "appX" subdomain.
I don't want to have to enter in hundreds of CNAMES,so am looking for a catch-all redirect for one sub-domain level to its parent.
Is there a way to do this with CNAME or does it require running an nginx proxy at prod.internalurl.us to make this work?
The solution that may help is simple enough. To find it out let me ask a question. Why do you need this functionality on DNS level?
What I'd like is:
.prod.internalurl.us to go to .internalurl.us
automatically.
CNAME doesn't help with conditional URL rewrite, there is no such logic on that layer. What helps is HTTP layer 301 redirect can be managed via Nginx:
server {
server_name ~^(?<app>.+)\.prod\.internalurl\.us$;
return 301 http://$app.internalurl.us$request_uri;
}
There is no proxy but HTTP 301 redirect instead.

How do I to forward example.com to www.example.com at godaddy for s3 hosted site?

I have:
signed up for an AWS account
set up buckets for example.com and www.example.com
enabled website settings in properties for each domain
set bucket policies for both domains
created this cname record:
www > www.example.com.s3-website-us-east-1.amazonaws.com
Right now when a user goes to www.example.com, they see the index page, and everything is working just fine. When they go to example.com, there is nothing but darknenss and silence because the page just doesn't load (and gives 'connection timeout' error. )
What I want is for example.com to forward to www.example.com so that no matter what the user types in the browser, they see the site that is located at www.example.com.
How do I make this work?
Can I add a record of some kind at godaddy to make example.com redirect to www.example.com?
Do I have to get a static IP to make this work using s3? I keep reading about an elastic IP from EC2, but I am not understanding how I can set that up, or how it would work since I am not using EC2 at all for my website.
I think user Go Daddy's answer was correct, but I'll write up more succinct instructions that I followed to get the forwarding to work for me. I don't know why others are suggesting wwwizer, because GoDaddy uses a 301-redirect too (but maybe it didn't used to?). By the way, this answer isn't specific to Amazon's S3 -- it simply requires you have the "www" CNAME record set correctly.
Login to GoDaddy.com.
Click on My Account.
Click on the Domains product, and click the Launch button next to your domain.
In the Domain Information section you should see "Forwarding: Off". Click the Manage link next to it.
A Forwarding and Masking dialog box should come up. Under Forward example.COM to enter www.example.com.
Click the OK button.
The forwarding kicked in for me within maybe half an hour.
To note, in the dialog box you can change it to forward either "http://" or "https://". I only needed it for "http://", so I'm not sure if GoDaddy will forward both SLL and non-SSL at the same time.
Also, if you're curious, if you click on the Advanced Options link in the Forwarding and Masking dialog box, it should be defaulted to "Forward Only" and have the redirect type set to permanently forward your domain (which is what gives the 301-redirect).
You can use wwwizer's free naked domain redirect service:
http://wwwizer.com/naked-domain-redirect
Just put 174.129.25.170 as the A record for the non-www version of your domain.
fragholrok,
I'm glad you were able to find a way to get your site working. Admittedly, it's a little hard to understand exactly what's going on with your domain name without more specific info, but I wanted to jump in and mention one thing.
You weren't sure if you could forward your domain to the www subdomain. It depends how you have it setup - sometimes it will work and sometimes it won't. According to your original post, the www subdomain is setup with its own CNAME (as opposed to mirroring the main A Record). Because of this, you COULD forward your domain to the www subdomain - ie, forward domain.com to www.domain.com - using GoDaddy.com's domain forwarding functionality.
If, however, the CNAME for a subdomain is set to '#', which means it directs to the main A Record, forwarding the domain to the subdomain would not work.
I just wanted to clarify for anyone else who might come across this thread.
Good luck with your site,
Alon
GoDaddy.com Social Media
Sounds like your DNS/Name Servers on GoDaddy aren't set up right. They should have the information listed for the Nameservers somewhere where you're hosting the site.
EDIT: I've done some more looking around and it would Amazon doesn't provide you the Nameservers for S3. The "real solution" to this would be to set up a CName on GoDaddy. This site provides a lot of insight on doing that: http://www.google.com/support/blogger/bin/answer.py?hl=en&answer=58317
Also, this question seems to have come up before: Using GoDaddy Domain Hosting to link to Amazon S3 Website
I used the approach described in
http://thechrisoshow.com/2011/06/05/how-to-host-a-static-website-on-s3/
(also with godaddy).
It boils down to deleting the A host entry, creating a CNAME entry and using godaddy's forwarding functionality.
For me it is a partial solution. It works if I type
jaumebarcelo.info
in the url bar.
But it does not work if I click on a link pointing to
jaumebarcelo.info
or if I type
wget jaumebarcelo.info from the command line.
I am looking for the same answer. How I have done it in the past is with an apache htaccess rewrite. I may do it again this way.
RewriteCond %{HTTP_HOST} ^mysite\.com
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=permanent,L]
Here is one such page that talks about it.
If you need more help I would google htaccess and apache rewrites.
If you are using Route 53, you can create a new bucket with your naked domain name, and under 'Static Website Hosting', set it to 'Redirect all requests to another host name'.
Update : Jul 2019
Since the original accepted answer by Michael Krebs in 2012, screens/ui might have changed little bit.
Please follow the screenshots below for the latest steps as of today:
Then click on add.
If no A record and www sub-domain is bound with subdomain it should redirect root domain to www sub-domain. This is how it works with many other sites.