Root domain redirect to www


#1

Is it possible with Cloudflare to redirect all requests coming to root domain (A Record) to www subdomain without even hitting my server?

So I have www subdomain setup served by S3 + Cloudfront (for SSL) and I just want to completely bypass my own servers (looks like I still need it for redirecting root domain to www)


#2

Yes! You probably already have a DNS entry for your root domain. Now, add a Page Rule:

Match: example.com/* and Redirect (301) to https://www.example.com/$1

Or http if you’re not using SSL. The $1 means to copy the path from the original request.

p.s. Make sure the root and www DNS entries are set to :orange:


#3

Thanks sdayman, I have exactly same setup, but when I run:

$ curl -I mydomain.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 15 May 2018 19:48:59 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://www.mydomain.com/

I see in my nginx logs:

xxx.xxx.xxx.xxx - - [15/May/2018:22:48:59 +0300] "HEAD / HTTP/1.1" 301 0 "-" "curl/7.58.0"

My root domain A record has enabled CloudFlare proxying. So this is really strange. Maybe CloudFlare actually isn’t proxying domains with A records.


#4

I only checked one of your zones, but it has Match: https://example.com as opposed to Match: example.com

Also if your record is orange clouded and you’re using public DNS for resolution you shouldn’t see Server: nginx in the response. So I suspect either the record isn’t orange clouded or you may be testing from a machine which isn’t using Cloudflare’s DNS for resolving that host name.


#5

I note that the curl command didn’t actually return any Cloudflare
headers, there should be a Server: cloudflare and a CF-RAY: header.

You unfortunately X’d out useful information, but I’ll bet the request
came from your IP and not a Cloudflare IP, is that correct? Perhaps
you’ve used a local hosts file or similar to override the DNS
resolution? Or perhaps you have your domain paused in Cloudflare?


#6

Thanks everyone, especially thedaveCA. I forgot I had /etc/hosts entry for this domain as I need to access it not only via HTTP/HTTPS.

Thanks again for your fast answers!


#7

It’s easy to forget. I know I’ve done it.


#8

This topic was automatically closed after 14 days. New replies are no longer allowed.