CrazyBaran
CrazyBaran

Reputation: 592

Azure CDN Microsoft Standard Rules Engine Rewrite URL for single-page-application

We try to configure Azure CDN on Microsoft Standard pricing to allow us rewrite Url to route all application routing to ./index.html. How to setup rules engine to rewrite url but left all js file as it is? All examples in google show how to do this on premium pricing but we like to do this on Microsoft Standard it is possible?

Upvotes: 22

Views: 9073

Answers (3)

Promise Preston
Promise Preston

Reputation: 28860

I had this challenge when working on setting up a static website on Azure blob storage with Azure Microsoft CDN.

Here's how I got it done.

If you want to achieve this using a Powershell script, then use the below script. The script also contains a rule for setting up Http to Https redirect:

# Define Variables
$RESOURCE_GROUP_NAME = MyResourceGroup
$PROJECT = MyProject
$CDN_PROFILE_NAME = MyCDNProfile

# Create a New Http to Https Redirect Rule
$RULE_CONDITION_1 = New-AzCdnDeliveryRuleCondition -MatchVariable 'RequestScheme' -Operator 'Equal' -MatchValue 'HTTP'
$RULE_ACTION_1 = New-AzCdnDeliveryRuleAction -RedirectType 'Moved' -DestinationProtocol 'Https'
$HTTP_TO_HTTPS_RULE = New-AzCdnDeliveryRule -Name 'HttpToHttpsRedirectRule' -Order 1 -Condition $RULE_CONDITION_1 -Action $RULE_ACTION_1

# Create a New SPA Rewrite Rule
$RULE_CONDITION_2 = New-AzCdnDeliveryRuleCondition -MatchVariable 'UrlFileExtension' -Operator 'LessThan' -MatchValue '1'
$RULE_ACTION_2 = New-AzCdnDeliveryRuleAction -SourcePattern '/' -Destination '/index.html'
$SPA_REWRITE_RULE = New-AzCdnDeliveryRule -Name 'SpaRewriteRule' -Order 2 -Condition $RULE_CONDITION_2 -Action $RULE_ACTION_2

# Set the CDN Delivery Policy with the CDN Delivery Rule(s)
$CDN_DELIVERY_POLICY = New-AzCdnDeliveryPolicy -Description "Https redirect policy" -Rule $HTTP_TO_HTTPS_RULE,$SPA_REWRITE_RULE

# Get CDN Endpoint
$CDN_ENDPOINT = Get-AzCdnEndpoint -ProfileName $CDN_PROFILE_NAME -ResourceGroupName $RESOURCE_GROUP_NAME -EndpointName $PROJECT

# Assign the CDN Delivery Policy to the CDN Endpoint
$CDN_ENDPOINT.DeliveryPolicy = $CDN_DELIVERY_POLICY

# Save CDN Endpoint Changes with Updated Delivery Policy
Set-AzCdnEndpoint -CdnEndpoint $CDN_ENDPOINT

This should output something like this for you:

enter image description here

Upvotes: 5

seabass
seabass

Reputation: 1120

I finally got a working answer from Microsoft Support on this. They said that the Microsoft CDN Rules Engine does not support URL file extension Not Any and instead I should check for the length of the file extension.

  • Condition: URL file extension
    • Operator = Not greater than
    • Extension = 0
    • Case Transform = No transform
  • Action: URL rewrite
    • Source Pattern = /
    • Destination = /index.html
    • Preserve unmatched path = No

This solution works for me. Make sure to purge your cdn cache before testing.

Upvotes: 39

limos
limos

Reputation: 1566

If none of your page URLs contain a dot, you can set a rule as follows:

  • Condition: 'URL file extension' = Not Any (i.e. there is no extension)
  • Action: 'URL rewrite',
    • source = /
    • destination = index.html
    • Preserve umatched path = No

This will rewrite any URL without a dot in the trailing section to index.html.

Upvotes: 5

Related Questions