How to Remove the Hosting Folder Name from MVC URL

I'm hosting several MVC websites on GoDaddy shared hosting. I noticed that the hosting folder name is being added by default to the URL. 

This is not an issue in GoDaddy shared hosting but it is a normal behavior when hosting the website in a virtual directory.

There are different ways to remove the hosting folder name from ASP.NET MVC URL. Here are some possible ways:

Use Virtual Directory:

A virtual directory is a folder that is not located in the website's physical directory structure but appears as if it is. You can create a virtual directory in IIS (Internet Information Services) and map it to the root folder of your ASP.NET MVC application. This allows you to access your application without including the hosting folder name in the URL. To create a virtual directory, follow these steps:

  • Open IIS Manager.
  • Right-click on the site you want to add a virtual directory to, and select "Add Virtual Directory".
  • Enter a name for the virtual directory, and specify the physical path to the root folder of your ASP.NET MVC application.
  • Click "OK" to create the virtual directory.

Use URL Rewrite Module:

The URL Rewrite module is an extension for IIS that allows you to modify the URLs of your website. You can use this module to rewrite URLs and remove the hosting folder name from the URL. To use the URL Rewrite module, follow these steps:

  • Install the URL Rewrite module on your server if it is not already installed.
  • Open IIS Manager.
  • Select the website you want to modify.
  • Click on the "URL Rewrite" icon in the IIS section.
  • Click "Add Rule(s)" to create a new rule.
  • Select "Blank Rule" and click "OK".
  • Enter a name for the rule.
  • Under the "Match URL" section, enter a pattern that matches the URL you want to rewrite.
  • Under the "Action" section, select "Rewrite" as the action type.
  • Enter the new URL in the "Rewrite URL" field, without including the hosting folder name.
  • Click "Apply" to save the rule.

On a shared hosting server, you can apply the previous steps directly in the web.config:

<rewrite>
  <rules>
    <rule name="Remove Virtual Directory">
      <match url=".*" />
      <action type="Rewrite" url="{R:0}" />
    </rule>
  </rules>
</rewrite>

This causes URL rewrite to add the original URL (the one with no folder name) to a ServerVariable which is used by ASP.NET MVC to generate URLs.

Use Route Mapping:

In ASP.NET MVC, you can use route mapping to define how URLs are handled by your application. You can use this technique to remove the hosting folder name from the URL. To use route mapping, follow these steps:

  • Open the "RouteConfig.cs" file in your ASP.NET MVC application.
  • Add a new route mapping by calling the "MapRoute" method.
  • Specify the URL pattern that you want to map.
  • Specify the controller and action that will handle the request.
  • Set the "RouteExistingFiles" property to "true".
  • Add any additional constraints or defaults as necessary.
  • Save the "RouteConfig.cs" file.

Summary

Having a clean and professional-looking URL is important for any content creator. Removing the hosting folder name from the ASP.NET MVC URL is a great way to achieve this. In this article, we have explored three possible ways to achieve this: using a virtual directory, using the URL Rewrite module, and using route mapping. Each of these techniques has its pros and cons, so it's up to you to choose the one that works best for your application. Regardless of the method you choose, removing the hosting folder name from the URL can improve the user experience and make your site more SEO-friendly.

Post a Comment

Previous Post Next Post