How remove localhostportno when running an asp net website under iis

Introduction

When running an ASP.NET website under IIS, it is common to see the port number appended to the URL. This can be undesirable for various reasons, such as aesthetics or security concerns. In this article, we will explore different to remove the localhost port number from the URL when running an ASP.NET website under IIS.

Approach 1: URL Rewrite

The URL Rewrite Module is an IIS that allows you to rewrite URLs based on defined rules. To remove the localhost port number, you can create a rule that matches the specific port number and to the same URL without the port number.


<system.webServer>
  <rewrite>
    <rules>
      <rule name="Remove Localhost Port Number" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
          <add input="{HTTP_HOST}" pattern="localhost:([0-9]+)" />
        </conditions>
        <action type="Redirect" url="http://{C:0}/{R:1}" redirectType="Permanent" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

This rule will match any URL with the localhost port number and redirect it to the same URL without the port number. The redirectType attribute is set to “Permanent” to indicate that this is a permanent redirect.

Approach 2: Using URL Rewrite Module with Regular Expression

If you want to remove the localhost port number for a specific port, you can use regular in the URL Rewrite Module. This approach allows you to define a more specific rule for removing the port number.


<system.webServer>
  <rewrite>
    <rules>
      <rule name="Remove Localhost Port Number" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
          <add input="{HTTP_HOST}" pattern="localhost:1234" />
        </conditions>
        <action type="Redirect" url="http://{C:0}/{R:1}" redirectType="Permanent" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

In this example, the rule will only match URLs with the specific port number 1234 and redirect them to the same URL without the port number.

Approach 3: Using Custom Code in .asax

If you prefer to handle the removal of the localhost port number within your ASP.NET application, you can use custom code in the Global.asax file. This approach gives you more flexibility and control over the URL rewriting process.

In this example, the Application_BeginRequest event handler checks if the host is “localhost” and the port is not 80. If so, it constructs a new URL without the port number and performs a redirect.

Conclusion

Removing the localhost port number when running an ASP.NET website under IIS can be achieved using different approaches. The URL Rewrite Module provides a convenient way to define rules for URLs, while custom code in the Global.asax file gives you more control over the URL rewriting process. Choose the approach that best suits your and preferences.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents