ASP.NET Core: Redirect to Proper Site URL as Stated in IIS

211 Views Asked by At

I have a site hosted at a URL like this: domain.com/myApp/

When a user goes to domain.com/myapp/ (lowercase a), I want it to redirect to domain.com/myApp/ (uppercase A), just as stated in IIS.

I need to deploy this code to multiple domains on multiple servers, and I need each one to redirect to its own correct URL with the correct case. So the correct URL must be read from some data source and not hard coded.

I am very close to being able to perform the redirection, but I don't know how to retrieve the site's URL from IIS. I have tried builder.Configuration[WebHostDefaults.ServerUrlsKey] and builder.WebHost.GetSetting(WebHostDefaults.ServerUrlsKey), and while they work locally, they return null on the server.

So how can an ASP.NET Core site know the exact hosted site URL (not just the URL in the request)? Alternatively, how can ASP.NET Core redirect to the correctly cased URL automatically? Thank you.

1

There are 1 best solutions below

3
On

If you want domain.com/myapp/ redirect to domain.com/myApp/. Why not create a simple rewrite rule using the rewrite module in IIS? Just like below:

    <rewrite>
        <rules>
            <rule name="redirect rule" stopProcessing="true">
                <match url="^myapp(.*)" />
                <action type="Redirect" url="myApp(.*)" />
            </rule>
        </rules>
    </rewrite>