Redirect Azure Static Web App from naked domain to www

2.1k views Asked by At

I have set up an Azure Static Web App with a custom domain. Both example.com and www.example.com are correctly set up and working.

I would now like to create some rule in staticwebapp.config.json so all requests coming in to https://example.com/* would redirect to https://www.example.com/*.

Is there a way to do this? I know I can do it with Front door or similar, but can I do it through rules in Static Web App?

2

There are 2 answers

1
David Naylor On

I'm not sure if it is possible with rules in staticwebapp.config.json but another option is to set a default domain:

https://learn.microsoft.com/en-us/azure/static-web-apps/custom-domain-default#set-a-default-domain

When you designate a custom domain as your app's default domain, requests to other domains are automatically redirected to the default domain. Only one custom domain can be set as the default.

This does also carry the path over with the redirect.

0
Heinrich Ulbricht On

My solution is not what you ask for but your question describes nearly exactly what I needed. So I leave a solution for my need ^^

I mainly want to redirect host to host, so https://example.com to https://www.example.com. I don't necessarily need the path.

I ended up creating a second static web app for https://example.com, added a dummy index.html and a staticwebapp.config.json with the following content:

{
    "routes": [
        {
            "route": "/*",
            "redirect": "https://www.example.com",
            "statusCode": 301
        }
    ]
}

This redirects https://example.com* to the root https://www.example.com. I didn't find a way to do it within a single static web app.

This doesn't carry the path over though. So https://example.com/path will also be redirected to https://www.example.com.