Report this

What is the reason for this report?

How to 301 redirect? (cloudflare)

Posted on April 14, 2022

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.

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!

Hi @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

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.

  1. Login into Cloudflare
  2. Select your domain
  3. Click Rules (Page Rules)
  4. Click Create Page Rule
  5. in URL (required), fill with: mydomain.com/*
  6. in Pick a Setting (required), select: Forwarding URL
  7. in Select status code (required), select: 301 - Permanent Redirect
  8. in Enter destination URL (required), fill with: https://www.mydomain.com/$1
  9. Click Save and Deploy Page Rule

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 rules

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.