I am using Azure Verizon CDN to publish my web application (an SPA) from Blob storage. I've successfully set up a custom domain and SSL.
I've added a rewrite rule in the CDN to redirect to the default document index.html. Basically, this takes the incoming request and inserts "index.html" between the URL path and any query strings.
So
mydomain.com/startup
goes to mydomain.com/startup/index.html
mydomain.com/homepage
goes to mydomain.com/homepage/index.html
and
mydomain.com/showuser/?userId=xxxxx
goes to mydomain.com/showuser/index.html/?userid=xxxxx
Which all seems to work well.
Existing rule to add default document
The URL in the address bar used by the SPA never requests an actual file, but code in the default document index.html does. And these requests for files are all failing with a 404 because I guess the rewrite rule is acting on these as well.
What I want is some way to not perform the URL rewrite if the URL includes a filename. The rules engine prevents me from adding such a condition when using the URL Rewrite feature - apparently you can't use any match conditions on the URL when trying to use the URL Rewrite feature.
Error when trying to use condition involving URL filename
I was searching for the answer to this as well. I ended up having to engage Azure support.
Unlike App Services and IIS, CDN doesn't have a way to rewrite the URL when a file doesn't exist. However, so long as your SPA's routes don't contain a .
, you can create a rewrite rule that only acts on URL paths that don't contain a .
. This will have the effect of rewriting naked path requests while leaving file requests alone.
First off, your CDN Profile needs to be the Verizon Premium CDN SKU in Azure. It's the only SKU that supports rewrite rules.
[^?.]*(\?.*)?$
. This pattern catches URL paths with .
in them, but doesn't care whether or not it's in the query string.origin_path/document.ext
. This is the tricky part. The Path is relative to the origin root. For instance, if your CDN endpoint's origin path is /origin_path
and you want to redirect to index.html
, you would enter origin_path/index.html
. This will always be the case if your CDN is backed by an Azure Storage Account and the origin points to a container.I have an addendum to Justin Gould's answer. With "IF Always" you will break purge requests from Azure. Fortunately, they have a specific User-Agent so you can match on that.
In addition, it will break HTTP → HTTPS redirection if you do that with another rule. Verizon won't let you match on "Request Scheme" to filter out those requests so you have to be sneaky and match on X-Forwarded-Proto.
Instead of "IF" "Always", you'll want to set:
"IF" → "Request Header Regex" → Name: "User-Agent" → "Does Not Match" → "ECPurge/*" → Ignore Case: checked
"AND IF" → "Request Header Literal" → Name: "X-Forwarded-Proto" → "Does Not Match" → "http" → Ignore Case: checked
Then, follow the rest of Justin's instructions.
Reference: https://github.com/Azure/azure-cli/issues/6722
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With