asp.net-coreurl-rewritingweb-configrules

ASP.NET Core rewrite rule for www https to https non-www using web.config


Say I want my website www.example.com redirected like this:

https://www.example.com -> https://example.com

How would one achieve this? Here is what I tried, but it does not work. I get an ERR_CONNECTION_RESET error from the browser even after clearing cache or using different browser. Does the app have to be restarted?

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <location path="." inheritInChildApplications="false">
        <system.webServer>
            <handlers>
                <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
            </handlers>
            <aspNetCore processPath="dotnet" arguments=".\example.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
            <rewrite>
                <rules>
                    <rule name="Redirect to non-www and HTTPS" enabled="true" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions logicalGrouping="MatchAny">
                            <add input="{HTTP_HOST}" pattern="^www\.example\.cz$" />
                            <add input="{HTTPS}" pattern="off" />
                        </conditions>
                        <action type="Redirect" url="https://example.cz/{R:1}" appendQueryString="true" redirectType="Permanent" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </location>
</configuration>
<!--ProjectGuid: 8b668df1-7e1a-4d97-bd06-2ba62f95e6f5-->

I did try consulting with Bing Copilot, which generated this code for me. I did try googling, but the results were unsatisfactory for my exact problem.


Solution

  • How would one achieve this? Here is what I tried, but it does not work. I get an ERR_CONNECTION_RESET error from the browser even after clearing cache or using different browser.

    Well, according to our scenario and descripion along with the shared url-rewrite configuration it seems your code is okay.

    However, you should verify couple of other things beside.

    Let's have have a look what else do you need o confirm in order to check your configuration.

    First of all, please check if your server has Rewrite module is installed in IIS. Its pre-requisite. Also ensure your IIS version supports rewrite rules and the AspNetCoreModuleV2 module.

    Most importnatly, if you are using hosting server (rented) confirm with your hosting provider, if they support rewrite rules or if they have any specific requirements or restrictions for that.

    Another cruicial is that, verify if the IIS server has a valid SSL/TLS certificate for example.com. Verify that the certificate is installed correctly and covers both www and non-www versions. Also make sure other redirect rules that might conflict or create infinite loops.

    Does the app have to be restarted?

    Whenever, you made any changes into IIS configuration it's a good idea to restart your web server to apply the modifications.

    Make sure, you have enabled logging for the rewrite module and check the logs for any errors or additional information. You can add the following lines inside the <rewrite>. So that you can get error details in case of any failure.

    I would higly suggest your to temporarily replace your rule with a simple redirect to isolate potential issues. For instance, instead of directly running your existing configuration, you can following and check if that works as expected:

    <rule name="Test Redirect" stopProcessing="true">
      <match url=".*" />
      <action type="Redirect" url="https://example.com/{R:0}" />
    </rule>
    

    Note: Please check the above steps carefully and then try again. If still have any concern on this, please refer to this official document.