DiscountASP.NET

Root redirection to subdirectory

In a previous post we showed you how to force a normal HTTP call to HTTPS. Today we’ll show you how to redirect from root to subdirectory.

The default page/homepage in your root directory would normally be displayed when someone makes an HTTP request to your site. But what if you installed an application in a subdirectory and want to use that application as a starting point? For example, WordPress is installed in a subdirectory, and you want the WordPress application to be the default starting point when someone types your URL.

There is sample code available in our Knowledge Base to redirect using .NET code. This requires the page to load and compile before the redirection happens. Now I want to show you redirection using the IIS manager/web.config instead. This should redirect the request faster as the redirection happens as soon as the HTTP request received by IIS.

Using IIS7 manager, connect to IIS and navigate to the URL rewrite module. Add a blank rule and name it as you wish. Only two variables are needed; match URL pattern and actions.

The parameters are as follows:

Match URL section:
Requested URL : Matches the pattern
Using : Regular Expression
Pattern : ^$

Action section:
Action Type : redirect
redirect URL : your relative subdirectory path e.g., if nopCommerce is installed in a subdirectory you would use /nopCommerce
redirect type : permanent

This will write to your web.config and add this section:

<system.webServer>
<rewrite>
<rules>
<rule name="redirect" stopProcessing="true">
<match url="^$" />
<action type="RedirectToSubdir" url="/nopCommerce" />
</rule>
</rules>
</rewrite>
</system.webServer>

That’s all there is to it.

Remember than any changes you make with IIS Manager are written to web.config, so overwriting web.config in the future can remove those changes.

Exit mobile version