I’ve my website and dns on digitalocean. I want to 301 redirect non-www urls to www urls. Currently I found answers for apache server, but not for cloudflare. Does anyone know how to do it? I don’t want to change nameservers to Cloudflare website.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Using Cloudflare Rules.
But first, you must make sure that your website has www urls on its settings, if not you will caught on redirect error loop.
In this example the website already using HTTPS (Lets Encrypt) and using Full Encryption Mode and disable Always Use HTTPS on Cloudflare SSL/TLS settings.
mydomain.com/*
Forwarding URL
301 - Permanent Redirect
https://www.mydomain.com/$1
If you already have another rule, make sure this rule being the first rule
This rule will catch any non-www with http or https request and redirect it into
https://www.mydomain.com/
, so no need to create Apache/nginx redirection rulesHi @NitinAngler,
I’ll recommend you to not add your 301 redirects on Cloudflare but on your Application.
@alexdo has already provided answers on how to do Nginx and Apache redirect but if you want to do redirect on your App Platform check the following questions:
https://www.digitalocean.com/community/questions/app-platform-how-can-i-redirect-non-www-to-www
https://www.digitalocean.com/community/questions/redirect-non-www-to-www-using-app-platform
Hello there,
If I understand this correctly you want the redirection to happen on DigitalOcean’s end and not Cloudflare since you do not want to change your nameservers to CF?
Are you using Apache/Nginx on your server? You can check this tutorial for more information:
https://www.digitalocean.com/community/tutorials/how-to-create-temporary-and-permanent-redirects-with-apache-and-nginx
Hope that this helps!