In my pursuit to use my root domain techbrett.net work properly within Azure for a static website, and HTTPS, I utilized Cloudflare’s CNAME flattening and an SSL cert purchased from Namecheap. In that process, I tried a variety of ways to force a redirect for the root domain to the https:// or https://www. equivalent, but I couldn’t get any of them to work. My knowledge of securing websites with SSL and accessing them via HTTPS is very limited (that’s part of why I’m doing this challenge). It’s possible that using the following method to redirect your website to HTTPS with an CDN managed cert might work (instead of bringing your own cert). Regardless, I noticed that from certain browsers when accessing techbrett.net it would display the page with an unsecure connection, like so:
I was alarmed at first, and originally thought there could be something wrong with the cert, or the SSL settings in Cloudflare. Or maybe something went wrong with the cert in the Azure Key Vault. I later discovered I had a link in the index.html file pointing to my blog using http:// instead of https://. I’ve fixed that, but in the meantime I discovered how to properly redirect HTTP requests to HTTPS within Azure.
First open up the CDN endpoint where you are hosting this static website. Make sure it’s the correct one by verifying your custom domains are within that endpoint and have Custom HTTPS enabled. Then click on Rules Engine on the left hand side.
Here is where we’ll create a new rule to enforce HTTPS by redirecting all Requests for HTTP to HTTPS. Click Add Rule + and then Add Condition + and select Request Protocol. For Operator select Equals and for Value select HTTP. Then click New Action + and select URL Redirect. For Type select Found (302) and Protocol select HTTPS. Leave Hostname, Path, Query String, and Fragment blank. Then click Save. Your final rule should look like this:
And here’s the entire rule with blank fields:
However, your CDN endpoint will have a cache version of these rules. You’ll want to Purge this CDN endpoint by going to the previous screen (where it lists your custom domains) and clicking the Purge icon at the top:
It will take a few minutes. You can check the status by clicking the Notification bell in the top right:
This ensures that your CDN is serving the most recent changes. You may want to clear the cache in your web browser as well, or try from a different browser or device. After enabling this rule, entering techbrett.net redirected properly to https://techbrett.net without fail!
Leave a Reply